• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

VB.net picture box viewbox error

I'm trying to make picture editing software.

I have three picture boxes right now they are original, altered and final.

This initializes the Original and Altered.
    Private Sub InitializeImage()
        Try
            pboriginal.Image = Nothing
            Reset()
            pboriginal.Image = Bitmap.FromFile(Files(FILENUMBER))
            lblfilename.Text = Files(FILENUMBER)
        Catch ex As System.IO.FileNotFoundException
            MessageBox.Show("There was an error. Check the path to the bitmap.")
        End Try
    End Sub

Open in new window

This reacts to the sliders for zoom and brightness:
    Private Sub pbModified_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbaltered.Paint
        If pboriginal.Image Is Nothing Then
            Return
        End If
        Dim brt As Single = sbbrightness.Value / 100
        Dim zoom As Double = sbZoom.Value / 100
        Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
                                        {New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
                                         New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
                                         New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
                                         New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
                                         New Single() {brt, brt, brt, 1.0, 1.0}})
        Dim wid As Integer = pboriginal.Image.Width
        Dim hgt As Integer = pboriginal.Image.Height
        Dim image_attr As New Imaging.ImageAttributes
        Dim rect As Rectangle = New Rectangle(0, 0, wid * zoom, hgt * zoom)
        image_attr.SetColorMatrix(cm)
        e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        e.Graphics.DrawImage(pboriginal.Image, rect, 0, 0, wid, hgt, GraphicsUnit.Pixel, image_attr)
    End Sub

Open in new window

This is the code to cut out where the viewfinder is viewing:
    Private Sub ViewFinderClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbaltered.MouseDown
        viewfinder.Location = New Point(e.X, e.Y)
        viewfinder.BringToFront()
        Dim rect As Rectangle = New Rectangle(e.X, e.Y, 100, 100)
        Dim bit As Bitmap = New Bitmap(pboriginal.Image, pboriginal.Width, pboriginal.Height)
        Dim cropBitmap As Bitmap
        cropBitmap = New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(cropBitmap)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
        pbfinalpicture.Image = cropBitmap
    End Sub

Open in new window


Currently when you click with the viewfinder it puts the cutout from the original photo if I change pboriginal to pbaltered it throws an error about how i have to use New and that the image is null.
0
Millkind
Asked:
Millkind
  • 6
  • 2
1 Solution
 
CodeCruiserCommented:
Is it this line

>Dim bit As Bitmap = New Bitmap(pboriginal.Image, pboriginal.Width, pboriginal.Height)

It could be that the first time, there is no image in pbaltered so its null. You can add a check and if its null then use original otherwise altered.
0
 
MillkindAuthor Commented:
Thats the problem the altered is always null.  Even though i can see it, alter it and change it. It still comes up null when i try to capture a part of it.
0
 
CodeCruiserCommented:
Ah. Its because you are using pbModified_Paint to draw the image in that altered picturebox so its Image property is always null. You may want to go the route of Graphics.FromImage so that you can assign an image to pbaltered rather than painting it directly.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MillkindAuthor Commented:
So the pbModified_Paint sub needs reworked completely using Graphics.Fromimage to assign the created image to the image of pbaltered?
0
 
MillkindAuthor Commented:
Got Zoom Working,
    Private Sub sbBrightness_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sbbrightness.ValueChanged, sbZoom.ValueChanged
        'ZoomImage()
        pbaltered.Refresh()
        Try
            pbaltered.Image = Zoom(pboriginal.Image, sbZoom.Value)
        Catch
        End Try
    End Sub
    Public Function Zoom(ByVal img As System.Drawing.Image, ByVal ZoomValue As Int32) As System.Drawing.Image
        Dim width As Int32 = Convert.ToInt32(img.Width * ZoomValue) / 100
        Dim height As Int32 = Convert.ToInt32(img.Height * ZoomValue) / 100
        'Create a new image based on the zoom parameters we require
        Dim zoomImage As New System.Drawing.Bitmap(img, width, height)

        'Create a new graphics object based on the new image
        Dim converted As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ZoomImage)

        'Clean up the image
        converted.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        'Return the zoomed image
        Return ZoomImage
    End Function

Open in new window

0
 
MillkindAuthor Commented:
And changed my viewfinder capture to this:

    Private Sub ViewFinderClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbaltered.MouseDown
        viewfinder.Location = New Point(e.X, e.Y)
        viewfinder.BringToFront()
        Dim rect As Rectangle = New Rectangle(e.X, e.Y, 100, 100)
        Dim bit As Bitmap = New Bitmap(pbaltered.Image, pbaltered.Width, pbaltered.Height)
        Dim cropBitmap As Bitmap
        cropBitmap = New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(cropBitmap)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
        pbfinalpicture.Image = cropBitmap

    End Sub

Open in new window


But it still clips from the original picture
0
 
MillkindAuthor Commented:
Still experimenting but would really like some help
0
 
MillkindAuthor Commented:
Got the cutout to work nicely.

    Private Sub picOriginal_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbaltered.MouseUp
        ''reposition viewfinder and bring to front
        viewfinder.Location = New Point(e.X, e.Y)
        viewfinder.BringToFront()
        ''create rectangle to cut out
        Dim src_rect As New Rectangle( _
            e.X, _
            e.Y, _
            100, _
            100)
        Dim dest_rect As New Rectangle( _
            0, 0, _
            src_rect.Width, _
            src_rect.Height)
        ''initalize bitmap
        Dim m_CroppedBm As Bitmap = New Bitmap(src_rect.Width, _
            src_rect.Height)
        Using gr As Graphics = Graphics.FromImage(m_CroppedBm)
            gr.DrawImage(pbaltered.Image, dest_rect, src_rect, _
                GraphicsUnit.Pixel)
        End Using

        pbfinalpicture.Image = m_CroppedBm
    End Sub

Open in new window


Zoom code works great.

 
   Public Function Zoom(ByVal img As System.Drawing.Image, ByVal ZoomValue As Int32) As System.Drawing.Image
        Dim width As Int32 = Convert.ToInt32(img.Width * ZoomValue) / 100
        Dim height As Int32 = Convert.ToInt32(img.Height * ZoomValue) / 100
        'Create a new image based on the zoom parameters we require
        Dim zoomImage As New System.Drawing.Bitmap(img, width, height)

        'Create a new graphics object based on the new image
        Dim converted As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ZoomImage)

        'Clean up the image
        converted.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        'Return the zoomed image
        Return ZoomImage
    End Function

Open in new window


Still working on getting the brightness code to work as a function with graphics.fromimage

 
   Function Bright(ByVal img As System.Drawing.Image, ByVal BrightValue As Int32) As System.Drawing.Image
        ' Brightness should be -1 (black) to 0 (neutral) to 1 (white)
        Dim brightimage As Bitmap
        Dim brt As Single = sbbrightness.Value / 100
        Dim g As Graphics = Graphics.FromImage(pboriginal.Image)
        Dim r As Rectangle = New Rectangle(0, 0, pboriginal.Width, pboriginal.Height)
        Dim colorMatrixVal As Single()() = { _
           New Single() {1, 0, 0, 0, 0}, _
           New Single() {0, 1, 0, 0, 0}, _
           New Single() {0, 0, 1, 0, 0}, _
           New Single() {0, 0, 0, 1, 0}, _
           New Single() {brt, brt, brt, 0, 1}}

        Dim colorMatrix As New Imaging.ColorMatrix(colorMatrixVal)
        Dim ia As New Imaging.ImageAttributes

        ia.SetColorMatrix(colorMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)

        g.DrawImage(brightimage, r, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
        Return BrightImage
    End Function

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now