I have been trying to find a faster way to convert a Format1bppIndexed bitmap to a Format32bppArgb.
Anyone have any ideas of a faster method?
Here the results/output:
orgBitmap.PixelFormat = Format1bppIndexed
Resolution = 200 Physical Size 1178 x 1653
-- Converting Image... --
ConvertToRGB [ Graphics.DrawImageUnscaled ] - 96 mils
ConvertToRGB2 [ Clone Bitmap ] - 67 mils
ConvertToRGB3 [ Lockbits / Marshal.Copy ] - 84 mils
NEW Bitmap.PixelFormat = Format32bppArgb
Here is my code:
Anyone have any ideas of a faster method?
Here the results/output:
orgBitmap.PixelFormat = Format1bppIndexed
Resolution = 200 Physical Size 1178 x 1653
-- Converting Image... --
ConvertToRGB [ Graphics.DrawImageUnscaled ] - 96 mils
ConvertToRGB2 [ Clone Bitmap ] - 67 mils
ConvertToRGB3 [ Lockbits / Marshal.Copy ] - 84 mils
NEW Bitmap.PixelFormat = Format32bppArgb
Here is my code:
VB.NET:
Public Function ConvertToRGB(ByVal original As Bitmap) As Bitmap
Dim sw As New Stopwatch
sw.Start()
Dim newImage As New Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb)
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
Dim g As Graphics = Graphics.FromImage(newImage)
g.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed
g.InterpolationMode = Drawing2D.InterpolationMode.Low
g.SmoothingMode = Drawing2D.SmoothingMode.None
g.DrawImageUnscaled(original, 0, 0)
sw.Stop()
Debug.Print("ConvertToRGB [ Graphics.DrawImageUnscaled ] - " & sw.ElapsedMilliseconds & " mils")
If newImage.PixelFormat <> PixelFormat.Format32bppArgb Then Debug.Print("WRONG FORMAT - newImage.PixelFormat = " & [Enum].GetName(GetType(PixelFormat), newImage.PixelFormat))
Return newImage
End Function
Public Function ConvertToRGB2(ByVal original As Bitmap) As Bitmap
Dim sw As New Stopwatch
sw.Start()
'' Note: This method does not work on Windows XP. Hmmm...
Dim newImage As Bitmap = Nothing
newImage = original.Clone(New Rectangle(0, 0, original.Width, original.Height), PixelFormat.Format32bppArgb)
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
sw.Stop()
Debug.Print("ConvertToRGB2 [ Clone Bitmap ] - " & sw.ElapsedMilliseconds & " mils")
If newImage.PixelFormat <> PixelFormat.Format32bppArgb Then Debug.Print("WRONG FORMAT - newImage.PixelFormat = " & [Enum].GetName(GetType(PixelFormat), newImage.PixelFormat))
Return newImage
End Function
Public Function ConvertToRGB3(ByVal original As Bitmap) As Bitmap
Dim sw As New Stopwatch
sw.Start()
' Lock source bitmap in memory
Dim sourceData As BitmapData = original.LockBits(New Rectangle(0, 0, original.Width, original.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
Dim imageSize As Integer = sourceData.Stride * sourceData.Height
' Create destination bitmap
Dim newImage As Bitmap = New Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb)
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
Dim sourceBuffer(imageSize) As Byte
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize)
' Unlock source bitmap
original.UnlockBits(sourceData)
' Lock destination bitmap in memory
Dim destinationData As BitmapData = newImage.LockBits(New Rectangle(0, 0, newImage.Width, newImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
' Copy binary image data to destination bitmap
Marshal.Copy(sourceBuffer, 0, destinationData.Scan0, imageSize)
' Unlock destination bitmap
newImage.UnlockBits(destinationData)
newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
sw.Stop()
Debug.Print("ConvertToRGB3 [ Lockbits / Marshal.Copy ] - " & sw.ElapsedMilliseconds & " mils")
If newImage.PixelFormat <> PixelFormat.Format32bppArgb Then Debug.Print("WRONG FORMAT - newImage.PixelFormat = " & [Enum].GetName(GetType(PixelFormat), newImage.PixelFormat))
Return newImage
End Function