Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / September 2006

Tip: Looking for answers? Try searching our database.

how to save grayscale image?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SharpCoderMP - 23 Aug 2006 18:00 GMT
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);
}
Bob Powell [MVP] - 23 Aug 2006 21:32 GMT
You can only save a GIF image in 8 bit. You can simulate gray scale by
creating a monochrom palette.

Signature

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.

> hi,
>
[quoted text clipped - 30 lines]
> b.Save(fileName, ici, ep);
> }
Michael C - 24 Aug 2006 02:14 GMT
> You can only save a GIF image in 8 bit. You can simulate gray scale by
> creating a monochrom palette.

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
SharpCoderMP - 24 Aug 2006 10:34 GMT
> 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.

can you post some code?
Michael C - 24 Aug 2006 12:28 GMT
>> 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.
>
> can you post some code?

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
SharpCoderMP - 24 Aug 2006 10:33 GMT
> You can only save a GIF image in 8 bit. You can simulate gray scale by
> creating a monochrom palette.

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
bitmap i get exceptions.
Michael C - 04 Sep 2006 00:26 GMT
> 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?

They do work when used correctly.

> i need a 8bit grayscale bitmap file. tiff or jpeg would be nice.
> 24bit image with simulated gray colors won't do the job.

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

> i also don't understand what for is a Bitmap(int,int,PixelFormat)
> constructor??

It creates bitmaps of different pixel formats.

> 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
> bitmap i get exceptions.

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2012 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.