HighTechTalks DotNet Forums  

Set PixelData of Bitmap by Pointer

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Set PixelData of Bitmap by Pointer in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
msedi
 
Posts: n/a

Default Set PixelData of Bitmap by Pointer - 10-27-2006 , 11:26 AM






Hello,

I have a short array

short[] RawData;

filled with medical image data. Each pixel is represented by a short
value, which must be windowed
by special settings. As I cannot use PixelFormat.Format16bppGrayScale
in GDI+, I tried to use the
PixelFormat.Format16bppRgb555, because I really need to draw into this
image and it would be an advantage to use the Graphics class functions
to draw into it.

The question now is that I want to keep the image data in my rawdata
array and I don't want to copy the data into the bitmap. I really want
to have let's say just a reference and all drawings I perform in the
Graphics canvas should be reflected to the rawdata as well.

Is there a way to do this? I tried following but I don't know if this
will work at all:

public Bitmap AsBitmap
{
get
{
MemoryStream m = new MemoryStream();

Bitmap BMP = new Bitmap(512, 512,
PixelFormat.Format16bppRgb555);
BitmapData Data = BMP.LockBits(new Rectangle(0, 0, 512,
512), ImageLockMode.ReadWrite, PixelFormat.Format16bppRgb555);

unsafe
{
fixed (short* p = &RawData[0])
{
short* ptr = p;

try
{
ptr = (short*)Data.Scan0.ToPointer();
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
}

BMP.UnlockBits(Data);

Graphics g = Graphics.FromImage(BMP);

g.DrawLine(new Pen(Color.White), 0, 0, 100, 100);
return BMP;
}
}


Thanks for your help
Martin


Reply With Quote
  #2  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: Set PixelData of Bitmap by Pointer - 11-01-2006 , 11:26 AM






ALL of the 16 bpp formats are damaged and unusable. They were included in
the final cut of GDI+ by mistake.

Sorry.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



"msedi" <msedi (AT) freenet (DOT) de> wrote

Quote:
Hello,

I have a short array

short[] RawData;

filled with medical image data. Each pixel is represented by a short
value, which must be windowed
by special settings. As I cannot use PixelFormat.Format16bppGrayScale
in GDI+, I tried to use the
PixelFormat.Format16bppRgb555, because I really need to draw into this
image and it would be an advantage to use the Graphics class functions
to draw into it.

The question now is that I want to keep the image data in my rawdata
array and I don't want to copy the data into the bitmap. I really want
to have let's say just a reference and all drawings I perform in the
Graphics canvas should be reflected to the rawdata as well.

Is there a way to do this? I tried following but I don't know if this
will work at all:

public Bitmap AsBitmap
{
get
{
MemoryStream m = new MemoryStream();

Bitmap BMP = new Bitmap(512, 512,
PixelFormat.Format16bppRgb555);
BitmapData Data = BMP.LockBits(new Rectangle(0, 0, 512,
512), ImageLockMode.ReadWrite, PixelFormat.Format16bppRgb555);

unsafe
{
fixed (short* p = &RawData[0])
{
short* ptr = p;

try
{
ptr = (short*)Data.Scan0.ToPointer();
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
}

BMP.UnlockBits(Data);

Graphics g = Graphics.FromImage(BMP);

g.DrawLine(new Pen(Color.White), 0, 0, 100, 100);
return BMP;
}
}


Thanks for your help
Martin




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.