I am stumped with how to properly save images I manipulated within my application. Simulated code of what I am trying to do is shown below. My problem is either the image quality suffers or the resulting byte array size becomes very big (more than 10x the original .jpg source file).
Any suggestions please. Thanks!
Any suggestions please. Thanks!
Code:
'>>>Load image file from disc
Dim bmp1 As New Bitmap("testimg01.jpg")
Me.PictureBox1.Image = bmp1 '<- benchmark image
'this is to simulate image manipulation within the application
Dim bmp2 As New Bitmap(bmp1.Width, bmp1.Height)
bmp2.SetResolution(300, 300)
Dim g As Graphics = Graphics.FromImage(bmp2)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(Me.PictureBox1.Image, New Rectangle(0, 0, bmp2.Width, bmp2.Height))
g.Dispose()
'---------------
'>>>this is to simulate saving the manipulated bitmap as byte array
Dim stream As New System.IO.MemoryStream
bmp2.Save(stream, ImageFormat.Jpeg)
' ??if I change the imageformat from Jpeg to Bmp, I get the right image quality but
' ??the size of the resulting memorystream buffer becomes 10x bigger than the original
Dim byte1() As Byte = stream.GetBuffer
stream.Close()
stream.Dispose()
bmp2.Dispose()
'---------------
'>>>this is to simulate restoring the saved array back to image
Dim stream2 As New System.IO.MemoryStream(byte1)
Dim bmp3 As Bitmap = Bitmap.FromStream(stream2)
'---------------
'>>>check image quality
' either the quality suffers if I use ImageFormat.Jpeg or
' the byte array size becomes outrageous if I use ImageFormat.Bmp
Me.PictureBox2.Image = bmp3
'---------------