How to "set" the PixelFormat for a bitmap?

J

johnb41

I need to use the bitmap.getpixel method to work on the pixel level of
an image. But GetPixel will not work with Indexed images. The images
that i work with are black & white text scans.

I create the bitmap object like this:
Dim b As Bitmap = Bitmap.FromFile("c:\scan.tif")

But I want to convert this file to PixelFormat.Format24bppRgb (or
something NOT indexed)

PixelFormat.Format24bppRgb is Read Only, so i can't simply do:
b.pixelFormat = PixelFormat.Format24bppRgb

Bitmap has an overload that allows the Pixelformat to be specified, but
the overload cannot accept a file name for my tiff, just a width and
height. If I use that overload, my bitmap object ends up blank. I
need it to hold image data.

I'm totally stumped. Can anyone help?

Thanks!
John
 
B

Bob Powell [MVP]

You must create a new bitmap of the same size as the indexed one and then
draw the indexed bitmap to the new bitmap. This will convert all the colours
to 32 bit and enable you to draw on the bitmap.

Bitmap bm=new Bitmap(org.Width, org.Height <,optional pixel format>);
bm.SetResolution(org.HorizontalResolution, org.VerticalResolution);
Graphics g=Graphics.FromImage(bm);
g.DrawImage(org,0,0);

//continue to draw on g here to add text or graphics.

g.Dispose();
bm.Save(......);

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

Bob Powell [MVP]

Sorry, that sample code was in C#. in VB...

Dim bm as new Bitmap(org.Width, org.Height <,optional pixel format>)
bm.SetResolution(org.HorizontalResolution, org.VerticalResolution)
Dim g as Graphics = Graphics.FromImage(bm)
g.DrawImage(org,0,0)

'continue to draw on g here to add text or graphics.

g.Dispose()
bm.Save(......)



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

John Buchmann

Bob,

Thanks, that helped alot! By the way, thanks for your web site. I've
learned alot from it the past couple weeks! :)

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top