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((Fil eArray(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(NewImag eA)
graphicsHandle.Interpolati onMode = InterpolationMode.HighQual ityBicubic
graphicsHandle.DrawImage(i mage, 0, 0, newWidth, newHeight)
End Using
Using graphicsHandle As Graphics = Graphics.FromImage(NewImag eB)
graphicsHandle.Interpolati onMode = InterpolationMode.HighQual ityBicubic
graphicsHandle.DrawImage(i mage, 0, 0, newWidth, newHeight)
End Using
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((Fil
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(NewImag
graphicsHandle.Interpolati
graphicsHandle.DrawImage(i
End Using
Using graphicsHandle As Graphics = Graphics.FromImage(NewImag
graphicsHandle.Interpolati
graphicsHandle.DrawImage(i
End Using
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...
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(File Array(x))
NewImageB.Save(tempfileloc )
image.Dispose()
Dim tempfileloc As String
tempfileloc = FileArray(x)
System.IO.File.Delete(File
NewImageB.Save(tempfileloc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked! Did not know I had to specify jpeg format. Thank you!