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
ProgrammingIsFunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
ProgrammingIsFunAuthor Commented:
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)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to use one of the other Bitmap.Save() overloads and specify a different format for the image.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ProgrammingIsFunAuthor Commented:
That worked! Did not know I had to specify jpeg format. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.