I have trawled the net to no avail with this. I want an 8 bit bmp with alpha that I can save as an 8 bit alpha png.
I am probably missing something simple here, but if i load the picturebox with a 24bit image then a 24bit image is saved. I set the create new bitmap in the maketransparent routine to 8 bit
here is my code
called with ConvertImageToByteArry, which should load the 8 bit conversion back into the picturebox. Then returns here to save it.
Can anyone give me any pointers here?
and subroutines (these are modified version of other peoples code on the net)Code:ConvertImageToByteArray() ' convert it and add transparency PictureBox1.Image.Save(My.Application.Info.DirectoryPath & "\pngs_out\" + filename, ImageFormat.Png)
Code:Private Sub ConvertImageToByteArray() Dim ms As New IO.MemoryStream PictureBox1.Image.Save(ms, Imaging.ImageFormat.Png) Dim bytes() As Byte = ms.GetBuffer() MakeTransparent(ms) PictureBox1.Image = Image.FromStream(ms) End SubCode:Private Function MakeTransparent(ByRef origBitmapMemoryStream As MemoryStream) As MemoryStream Dim transparentColor As Color = Color.White Dim transparentArgb As Int32 = transparentColor.ToArgb() Dim origBitmap As Bitmap = New Bitmap(origBitmapMemoryStream) 'Dim newBitmap As Bitmap = New Bitmap(origBitmap.Width, origBitmap.Height, origBitmap.PixelFormat) Dim newBitmap As Bitmap = New Bitmap(origBitmap.Width, origBitmap.Height, PixelFormat.Format8bppIndexed) Dim origPalette As ColorPalette = origBitmap.Palette Dim newPalette As ColorPalette = newBitmap.Palette Dim index As Int32 = 0 Dim transparentIndex As Int32 = -1 For Each origColor As Color In origPalette.Entries newPalette.Entries(index) = Color.FromArgb(255, origColor) If (origColor.ToArgb.Equals(transparentArgb)) Then transparentIndex = index End If index += 1 Next If transparentIndex.Equals(-1) Then Return origBitmapMemoryStream newPalette.Entries(transparentIndex) = Color.FromArgb(0, transparentColor) newBitmap.Palette = newPalette Dim rect As Rectangle = New Rectangle(0, 0, origBitmap.Width, origBitmap.Height) ' Dim origBitmapData As BitmapData = origBitmap.LockBits(rect, ImageLockMode.ReadOnly, origBitmap.PixelFormat) Dim newBitmapData As BitmapData = newBitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed) For y As Integer = 0 To origBitmap.Height - 1 For x As Int32 = 0 To origBitmap.Width - 1 Dim origBitmapByte As Byte = Marshal.ReadByte(origBitmapData.Scan0, origBitmapData.Stride * y + x) Marshal.WriteByte(newBitmapData.Scan0, newBitmapData.Stride * y + x, origBitmapByte) Next Next newBitmap.UnlockBits(newBitmapData) origBitmap.UnlockBits(origBitmapData) Dim m As MemoryStream = New MemoryStream() newBitmap.Save(m, ImageFormat.Bmp) newBitmap.Dispose() origBitmap.Dispose() Return m End Function


Reply With Quote
