L
Lance
I need to create a Drawing.Bitmap from an array of integer
values. My current technique creates a bitmap that
eventually becomes corrupt (i.e., the bitmap's pixels
change to a different color after a while). Can somebody
please tell me what I'm doing wrong? Here is a sample:
Public Shared Function CreateBitmapFromArray( _
ByVal width As Integer, _
ByVal height As Integer, _
ByVal pixelFormat As Drawing.Imaging.PixelFormat, _
ByVal pixelValues() As Integer) As Drawing.Bitmap
'Get a pinned handle of pixelValues.
Dim pixelValuesHandle As
System.Runtime.InteropServices.GCHandle = _
System.Runtime.InteropServices.GCHandle.Alloc
(pixelValues, Runtime.InteropServices.GCHandleType.Pinned)
'Get a pointer to the pixelValues that is used to
create the bitmap.
Dim scan0 As System.IntPtr =
pixelValuesHandle.AddrOfPinnedObject
'Get the stride of the resulting bitmap.
Dim stride As Integer = GetStride(width, pixelFormat)
'Create a bitmap from pixelValues.
Dim bitmap As New Drawing.Bitmap(width, height,
stride, pixelFormat, scan0) 'Return value.
'Release the handle.
pixelValuesHandle.Free()
Return bitmap
End Function
Thanks,
Lance
values. My current technique creates a bitmap that
eventually becomes corrupt (i.e., the bitmap's pixels
change to a different color after a while). Can somebody
please tell me what I'm doing wrong? Here is a sample:
Public Shared Function CreateBitmapFromArray( _
ByVal width As Integer, _
ByVal height As Integer, _
ByVal pixelFormat As Drawing.Imaging.PixelFormat, _
ByVal pixelValues() As Integer) As Drawing.Bitmap
'Get a pinned handle of pixelValues.
Dim pixelValuesHandle As
System.Runtime.InteropServices.GCHandle = _
System.Runtime.InteropServices.GCHandle.Alloc
(pixelValues, Runtime.InteropServices.GCHandleType.Pinned)
'Get a pointer to the pixelValues that is used to
create the bitmap.
Dim scan0 As System.IntPtr =
pixelValuesHandle.AddrOfPinnedObject
'Get the stride of the resulting bitmap.
Dim stride As Integer = GetStride(width, pixelFormat)
'Create a bitmap from pixelValues.
Dim bitmap As New Drawing.Bitmap(width, height,
stride, pixelFormat, scan0) 'Return value.
'Release the handle.
pixelValuesHandle.Free()
Return bitmap
End Function
Thanks,
Lance