Link to home
Start Free TrialLog in
Avatar of ProgrammingIsFun
ProgrammingIsFun

asked on

Creating two copies of original bitmap in VB. One copy does not work as expected.

I am writing a program to alter an image. One bitmap object is created and given a jpg. Two copies are made of that original image for editing. One copy(NewImageA) is resized and then posterized. The second copy (NewImageB) is resized and then saved into the original location and a second location.

NewImageA works as expected but NewImageB (which is only resized and saved) does not work as expected. Instead of resizing down between 30 kb and 150kb, it is reduced to 1mb to 2mb. Does anyone have an idea why this happens?

            Dim image As New System.Drawing.Bitmap((FileArray(x)).ToString)
            Dim originalWidth As Integer = image.Width
            Dim originalHeight As Integer = image.Height
            Dim percentbaseWidth As Single = CSng(1300) / CSng(originalWidth)
            Dim percentbaseHeight As Single = CSng(Size.Height) / CSng(originalHeight)
            Dim percentbase As Single = If(percentbaseHeight < percentbaseWidth,
                   percentbaseHeight, percentbaseWidth)
            Dim newWidth As Integer
            Dim newHeight As Integer
            newWidth = CInt(originalWidth * percentbase)
            newHeight = CInt(originalHeight * percentbase)
           
            Dim NewImageA As New Bitmap(newWidth, newHeight)
            Dim NewImageB As New Bitmap(newWidth, newHeight)

            Using graphicsHandle As Graphics = Graphics.FromImage(NewImageA)
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
            End Using

            Using graphicsHandle As Graphics = Graphics.FromImage(NewImageB)
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
            End Using
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

We'd need to see the code that actually saves the files to the drive to find the problem.  As it is, all we see is the creation of two bitmaps in memory...
Avatar of ProgrammingIsFun
ProgrammingIsFun

ASKER

Sure. Here are the lines emptying the original bitmap and then saving the NewImage. Note that I have switched out NewImageB with NewImageA to test that one and A works.

                image.Dispose()
                Dim tempfileloc As String
                tempfileloc = FileArray(x)
                System.IO.File.Delete(FileArray(x))
                NewImageB.Save(tempfileloc)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked! Did not know I had to specify jpeg format. Thank you!