473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Create a Bitmap from an Array

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

Nov 20 '05 #1
5 7333
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

------------------------

"Lance" <zi***@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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

Nov 20 '05 #2
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

-----Original Message-----
Hi,

Not sure if this will help. This is how you create an bitmap froman 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

------------------------

"Lance" <zi***@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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

.

Nov 20 '05 #3
* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
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


"Lance" <zi***@hotmail.com> wrote in message
news:0f****************************@phx.gbl...
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

-----Original Message-----
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

------------------------

"Lance" <zi***@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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

.

Nov 20 '05 #5
Cor
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
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

-----Original Message-----
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

------------------------

"Lance" <zi***@hotmail.com> wrote in message
news:09****************************@phx.gbl...
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

.

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Prashant | last post by:
Hi, I have a huge problem. I have a data file which looks something like this -: ..1 .5 .9 -1 .2 .5 ...... ..2 .9 .1 .4 .3 -1 ...... ..2 .4 .5 .7 .6 .2 ...... ........
1
by: Sharon | last post by:
I have 2 questions that are just the opposite to one another: (1) I need to read an image file (like bitmap, jpeg etc.) and to save only its data, I need to save his data in a raw data format,...
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
3
by: Iwanow | last post by:
Hello! My goal is to develop a program that opens a bitmap, copies its pixels to an ArrayList in order to perform some complex calculations (edge detection, Hough transform etc.), and save...
0
by: bcutting | last post by:
I have the following snippet of code which which makes a call into a dll to generate an array of bytes. Its arguments are a IntPtr to the bitmap, a number, and a reference to the array that it...
0
by: news.microsoft.com | last post by:
Hi guys, This text looks long and complicate but it is not, and I really really need some help here. For the last 24hs I'm trying to resolve this issue (last night I dreamed
2
by: =?Utf-8?B?UHJ6ZW1v?= | last post by:
Hi, I would like to have a web service method sending a bitmap image. But code like do not work: <WebMethod()_ Public Function GetBitmap() As Drawing.Bitmap Dim a As New...
5
by: stef | last post by:
hello I can find all kind of procedures to convert an array to a bitmap (wxPython, PIL), but I can't find the reverse, either - convert a bitmap to an array or - read a bitmap file to an...
3
by: radu | last post by:
i need a bit of help with few lines of code. 1. i have integer array a = numbers from 0 to 256 2. i have to create a 256 colours bitmap of 500x500 pixels, asign colours as matrix a. ex. bitmap at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.