Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+
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 19-Jun-12 4:48am
lzeng19861.3K
Comments
sunilsvsnlr2007 at 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 at 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!
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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+.
  Permalink  
v4
Comments
Sergey Alexandrovich Kryukov at 19-Jun-12 14:30pm
   
Really? Interesting, I'll need to check it up, thank you for sharing...
--SA
Muhtar Qong at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
}
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 DamithSL 320
1 OriginalGriff 155
2 Peter Leow 115
3 Afzaal Ahmad Zeeshan 114
4 deepakdynamite 110
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 4,994
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 19 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100