all groups > dotnet drawing api > august 2006 >
You're in the

dotnet drawing api

group:

how to save grayscale image?


how to save grayscale image? SharpCoderMP
8/23/2006 7:00:49 PM
dotnet drawing api:
hi,

how can i save a grayscale (8bit) image? i tried using
System.Drawing.Imaging.Encoder.ColorDepth but when i try to save jpeg
wither using 8 or 1bit i always get 24bit one. when i try to save tiff i
get an exception when i try 8 or 1bit. what's going on??

here's code example:

using (Bitmap b = new Bitmap(200, 50))
using (Graphics g = Graphics.FromImage(b))
using (Font f = new Font(FontFamily.GenericSansSerif, 12))
using (StringFormat sf = new StringFormat())
{
g.PageUnit = GraphicsUnit.Pixel;
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Rectangle rect = new Rectangle(0, 0, b.Width, b.Height);
g.FillRectangle(Brushes.White, rect);
g.DrawString(someText, f, Brushes.Black, rect, sf);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, (long)85);
ep.Param[1] = new EncoderParameter(
System.Drawing.Imaging.Encoder.ColorDepth, 8L);
b.Save(fileName, ici, ep);
Re: how to save grayscale image? Bob Powell [MVP]
8/23/2006 10:32:26 PM
You can only save a GIF image in 8 bit. You can simulate gray scale by
creating a monochrom palette.

--
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.



[quoted text, click to view]

Re: how to save grayscale image? Michael C
8/24/2006 12:00:00 AM
[quoted text, click to view]

I can save an 8 bit image to tiff on my machine.

To the OP, your problem is that the bitmap is in 24 or 32 bit format. You
need to make it an 8 bit indexed image before saving.

Michael

Re: how to save grayscale image? SharpCoderMP
8/24/2006 12:00:00 AM
[quoted text, click to view]

you mean indexed palette?
i don't get it. there are options for encoders, like Encoder.ColorDepth
but they don't work?? what for are they then?
i need a 8bit grayscale bitmap file. tiff or jpeg would be nice.
24bit image with simulated gray colors won't do the job.

i also don't understand what for is a Bitmap(int,int,PixelFormat)
constructor?? if i use something else than RGB i cant draw anything to
that bitmap. every time i try to create a Graphics object from such
Re: how to save grayscale image? SharpCoderMP
8/24/2006 12:00:00 AM
[quoted text, click to view]

Re: how to save grayscale image? Michael C
8/24/2006 9:28:18 PM
[quoted text, click to view]

The code you have should work fine for saving the image, I had the same code
(without the quality setting) but to tiff. You need to convert the image to
8 bit first.

int width = SourceBitmap.Width;
int height = SourceBitmap.Height;
BitmapData srcData = SourceBitmap.LockBits(new Rectangle(0, 0, width,
height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
BitmapData dstData = DestinationBitmap.LockBits(new Rectangle(0, 0, width,
height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int srcOffset = srcData.Stride - 4 * width;
int dstOffset = dstData.Stride - width;
int* ptrSrc = (byte*)srcData.Scan0;
byte* ptrDst = (byte*)dstData.Scan0;
for(int y = 0; y < height; y++, ptrSrc += srcOffset, ptrDst += dstOffset)
{
for(int x = 0; x < width; x++, ptrSrc++, ptrDst++)
{
Color c = Color.FromArgb(*ptrSrc & 0x00ffffff);
*ptrDst = TranslateColor24BitTo8Bit(c);
}
}
SourceBitmap.UnlockBits(srcData);
DestinationBitmap.UnlockBits(dstData);

Michael

Re: how to save grayscale image? Michael C
9/4/2006 12:00:00 AM
[quoted text, click to view]

They do work when used correctly.

[quoted text, click to view]

An indexed palette is still an 8 bit image, not 24. The palette contains 256
entries all of which are shades of gray.

[quoted text, click to view]

It creates bitmaps of different pixel formats.

[quoted text, click to view]

That is correct for some formats. You can't paint onto a 8 bit indexed
bitmap because I presume it would be too complicated for the graphics object
to modify the palette to suit the painting being done.

AddThis Social Bookmark Button