Question Are Cloned Images Disjoint From original Image?

scdevils2

Member
Joined
Jun 13, 2007
Messages
7
Programming Experience
3-5
If I set CloneFirst =True, the image is displayed as expected. If i Set CloneFirst =False the image is displayed pixelated. Can someone please explain to me why this is happening.

VB.NET:
    Private Const FILE_PATH As String = "staticmap.jpeg"
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim imageFile As New IO.FileInfo(FILE_PATH)
        Dim newBitMap As Bitmap = CType(Bitmap.FromStream(imageFile.OpenRead), Bitmap)
        Dim clonedBitMap As Bitmap

        clonedBitMap = getBitMap(newBitMap, ckbCloneFirst.Checked)
        pbImage.Image = clonedBitMap



    End Sub


    Private Function getBitMap(ByVal BottomImage As Bitmap, ByVal CloneFirst As Boolean) As Bitmap

        Dim CopiedBottomImage As Bitmap = DirectCast(BottomImage.Clone(), Bitmap)
        Dim CopiedImageGraphics As Graphics
        Dim otherImage As Bitmap


        If CloneFirst Then
            otherImage = DirectCast(BottomImage.Clone(), Bitmap)
            CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
        Else
            CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
            otherImage = DirectCast(BottomImage.Clone(), Bitmap)
        End If

        'otherImage.Dispose()
        'BottomImage.Dispose()
        '' secondImage.Dispose()
        'CopiedImageGraphics.Dispose()

        Return CopiedBottomImage
    End Function
 
If I set CloneFirst =True, the image is displayed as expected. If i Set CloneFirst =False the image is displayed pixelated.
I don't see how, your getBitMap method returns BottomImage.Clone no matter what CloneFirst parameter is.
Are Cloned Images Disjoint From original Image?
Yes.

You have commented out some Dispose calls here, Graphics instances created from FromImage must be disposed when done using them, image/bitmap objects must also be disposed after use. So otherImage.Dispose() and CopiedImageGraphics.Dispose() is needed in getBitMap method. In btnDisplay_Click method you need to dispose the image object held by newBitMap variable, and if pbImage.Image already contains an image you must dispose that before assigning it a new image object. imageFile.OpenRead returns a FileStream object, you have to close/dispose this when done using it also. In general you have to analyse your code to make sure you don't open/create objects and just leave them in memory after last reference is released without disposing/closing them properly.
 
yes, I know you have to dispose of the image after the use, I was commenting out the code trying to figure out what was going on.

So I tried Saving the Image to demonstrate what I am seeing, however, when i save the image, the image does not display incorrectly any more.

VB.NET:
    Private Function getBitMap(ByVal BottomImage As Bitmap, ByVal CloneFirst As Boolean) As Bitmap

        Dim CopiedBottomImage As Bitmap = DirectCast(BottomImage.Clone(), Bitmap)
        Dim CopiedImageGraphics As Graphics
        Dim otherImage As Bitmap


        If CloneFirst Then
            otherImage = DirectCast(BottomImage.Clone(), Bitmap)
            CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
        Else
            CopiedImageGraphics = Graphics.FromImage(CopiedBottomImage)
            otherImage = DirectCast(BottomImage.Clone(), Bitmap)
        End If

        otherImage.Dispose()
        CopiedImageGraphics.Dispose()
        'Fixes the Problem
        CopiedBottomImage.Save(SAVE_IMAGE_PATH)
        Return CopiedBottomImage
    End Function
 
Here is what i am seeing, when I dont have the save code in there
 

Attachments

  • imageNormal.JPG
    imageNormal.JPG
    69.8 KB · Views: 31
  • imagePixelated.JPG
    imagePixelated.JPG
    36.8 KB · Views: 31
Back
Top