JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Handling Images

To handle images using the Graphics class, you can use methods like DrawImage, which draws images where you want them. We've already seen an example showing how that works; that example is named ScrollImage in Chapter 8 (see "Scrolling Images" in the Immediate Solutions section of that chapter). Now that we know more about the Graphics class, I'll take another look at that example's code here. There are many overloaded forms of DrawImage; with the one I'm using here, you pass the Image object to work with, the destination rectangle to draw in, the source rectangle to copy pixels from, and the graphics units (which are pixels). To scroll the image, you use the scrollbar positions and simply redraw the image, like this:

Imports System.Drawing
Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub ShowScrollBars()
        VScrollBar1.Visible = True
        HScrollBar1.Visible = True

        If PictureBox1.Height > PictureBox1.Image.Height Then
            VScrollBar1.Visible = False
        End If

        If PictureBox1.Width > PictureBox1.Image.Width Then
            HScrollBar1.Visible = False
        End If
    End Sub

    Public Sub ScrollBars_Scroll(ByVal sender As Object, _
        ByVal se As ScrollEventArgs)
        Dim graphics As Graphics = PictureBox1.CreateGraphics()
        graphics.DrawImage(PictureBox1.Image, New Rectangle(0, 0, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), _
            New Rectangle(HScrollBar1.Value, VScrollBar1.Value, _
            PictureBox1.Width - HScrollBar1.Height, _
            PictureBox1.Height - VScrollBar1.Width), GraphicsUnit.Pixel)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler HScrollBar1.Scroll, AddressOf ScrollBars_Scroll
        AddHandler VScrollBar1.Scroll, AddressOf ScrollBars_Scroll
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() <> DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            HScrollBar1.Maximum = PictureBox1.Image.Width - _
            VScrollBar1.Maximum = PictureBox1.Image.Height - _
        End If
    End Sub
End Class

Note that all I'm doing is to use the picture box's CreateGraphics method to create a Graphics object (recall that because CreateGraphics is a Control class method, it's supported in Windows forms and controls), and then using that Graphics object to scroll the image. That's all it takes. You can see this example at work in Figure 13.7.

Figure 13.7: Scrolling an image using the DrawImage method.

Related solution:

Found on page:

Scrolling Images


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor