Click here to Skip to main content
15,868,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I draw the bitmap using Graphics.DrawImage(),it occurre an error.I had to convert the 16bppGrayScale bitmap to 8bppIndexed bitmap first,then convert it to 24bppRgb bitmap,now the GDI+ can draw it correctly.So,I would like to ask whether there is an way to draw 16bppGrayScale bitmap directly using GDI+.Thanks.
Posted
Comments
sunilsvsnlr2007 19-Jun-12 10:04am    
hi,

but bitmap image creation code you must need to Write in _paint() method of the any control like panel,form etc and here is the code

Image bmp = new Bitmap(200, 40, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);

using (Graphics g = Graphics.FromImage(bmp))
{
g.FillRectangle(Brushes.White, 0, 0, Width, Height);

panel1_Paint(null, new PaintEventArgs(g, this.ClientRectangle));

bmp.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);
}
lzeng1986 19-Jun-12 10:12am    
Yes,I write the code in the OnPaint event:

protected overridevoid OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(...);
}
but it failed yet!
}

There is no a way to draw 16bppGrayScale bitmap directly using GDI+.
Because you can not get Graphics object from Format16bppGrayScale image.
Namely:

Bitmap bitmap = new Bitmap(256, 256, PixelFormat.Format16bppGrayScale);
Graphics g = Graphics.FromImage((Image)bitmap);

This will throw error.

You are doing correct in expensive of degrading radiometric resolution of the image.
My mean is the dynamic range (pixel value range) of the Format16bppGrayscale is should be 0-65535 (However, you are converting this value range to 0-255: Format8bppIndexed).

Actually, in .NET it is only 0-8192. This should be in the range of 0-65535(This is something wrong in .NET's Format16bppGrayscale image format).

If you are trying to draw the image in Format16bppGrayscale without radiometric resolution lost, please create a Format48bppRgb image from your Format16bppGrayscale image, then, draw it by using GDI+.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 19-Jun-12 14:30pm    
Really? Interesting, I'll need to check it up, thank you for sharing...
--SA
Muhtar Qong 20-Jun-12 1:45am    
Yes, I have experienced it many times. Every time I created 16bppGrayScale image some places which have high values have been saturated (cut off). Then, I checked it up and found that pixel values were in the range of 0-8192 even I entered more high values. However, I have experienced it in VS 2008.
hi,

but bitmap image creation code you must need to Write in _paint() method of the any control like panel,form etc and here is the code

Image bmp = new Bitmap(200, 40, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);

using (Graphics g = Graphics.FromImage(bmp))
{
g.FillRectangle(Brushes.White, 0, 0, Width, Height);

panel1_Paint(null, new PaintEventArgs(g, this.ClientRectangle));

bmp.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jun-12 10:28am    
The main idea about using Bitmap and Graphics.FromImage is correct, but what you write about _paint() is poor gibberish and irrelevant to the question. (There is not such event; do you really understand how events are handled? If you do, where is your "+="? Otherwise how OP would understand how to do it? Anyway, this is irrelevant to bitmap which you can do without any forms or windows.)
Sorry, I can vote 3 no more...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900