Create a Bitmap from an Array

  • Thread starter Thread starter Lance
  • Start date Start date
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
 
Hi,

Not sure if this will help. This is how you create an bitmap from
an byte array.

Dim bm As Bitmap

Dim arByte() As Byte = objData ' Your data

Dim ms As New System.IO.MemoryStream

ms.Write(arByte, 0, arByte.Length)

bm = New Bitmap(ms)

Ken
 
Thanks, but what I really need to know is how to create a
bitmap from an array of integer values using the
following Bitmap constructor:

Public Sub New( _
ByVal width As Integer, _
ByVal height As Integer, _
ByVal stride As Integer, _
ByVal format As PixelFormat, _
ByVal scan0 As IntPtr _
)

Lance
 
* "Ken Tucker said:
Not sure if this will help. This is how you create an bitmap from
an byte array.

Dim bm As Bitmap

Dim arByte() As Byte = objData ' Your data

Dim ms As New System.IO.MemoryStream

ms.Write(arByte, 0, arByte.Length)

bm = New Bitmap(ms)

Notice that this will require the whole bitmap file to be stored inside
the byte array.
 
This constructor referrs to the data that would be found in a BitmapData
object after a lockbits method had been called.

The GDI+ FAQ has an article on LockBits and explains the data structure in
the BitmapData object. Perhaps that will help y0u out.

--
Bob Powell

Get a free issue of Well Formed VB edition.
http://www.bobpowell.net/vb_offer.htm

What's Well Formed?
http://www.bobpowell.net/currentissue.htm

Visit the GDI+ FAQ for articles, tips, tricks and code.
http://www.bobpowell.net/gdiplus_faq.htm

Read my blog
http://bobpowelldotnet.blogspot.com
 
Hi Lance,

This would do it I think, but you have to check it firm, because it is
pasted from everywhere (I have it in all kind of functions) so see it as a
roughly made example (nothing with stride because I do not use that)
\\
Dim originalImage As Image
Dim ms As New MemoryStream(arrPicture)
originalImage = Image.FromStream(ms)
Dim originalRect
Dim pictureRect As Rectangle
'set the rectangle properties
bmpCropped = New Bitmap(picturerect.Width, picturerect.Height)
Dim grBitmap As Graphics = Graphics.FromImage(bmpCropped)
grBitmap.DrawImage(originalImage, pictureRect, originalRec,
GraphicsUnit.Pixel)
picToShow.Image = bmpCropped
//

I hope this helps,

Cor


"> Thanks, but what I really need to know is how to create a
 
-----Original Message-----
This constructor referrs to the data that would be found in a BitmapData
object after a lockbits method had been called.

Yep, that tells me what I needed to know. Lockbits it is
then. Thanks!
 
Back
Top