problems saving a bitmap

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm trying to save a bitmap file so I can print it later. I have
Image.Save("sImage.bmp",System.Drawing.Imaging.ImageFormat.Bmp);

The file sImage.bmp shows up in the bin\debug folder but when I try to print
it (or open it), it's completely black. I know Image is the correct picture
because when I immediately print it, it works. What am I doing wrong?

Thanks!
Melanie
 
Melanie,

When you create the bitmap, how many bits per pixel are you setting it
for? I think by default, it uses 32 bpp, but most viewers can only handle
24 bpp. Change the settings on the bitmap (in code) and you should be able
to see it fine.

Hope this helps.
 
I suspect that you're drawing on the image with a black pen. Perhaps
creating a text bitmap?

When you create an image it will be a 32bit per pixel image with all the
pixel elements, including the alpha, set to zero. When saved this will look
like a black image. If you draw on it in black before saving it it will be
black ink on a black background (ie.. all black)

Before drawing on the image use the Graphics.Clear method to clear the image
to some sensible colour (white is good)

Then you'll see your image.

--
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 again,
I'm actually not drawing on the bitmap, I'm capturing the screen of the
particular tabpage that I'm focused on. On the last tabpage, I have the
option of printing any of the previous four pages. My problem arises when I
load the form and don't focus on one of the screens I want to print (I have
an event that captures each screen as a bitmap when focus leaves). So when
I'm actually on a screen, I've been trying to save it to a file that can be
called some time in the future if the bitmap is null (hasn't been focused
on).
Any thoughts?
Thanks again!!!
Melanie
 
Check out the GDI+ FAQ. There is an article there that captures a screenshot
or a shot of a specific control.

Without seeing your own code I'd be hard-pressed to suggest a solution.

--
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.
 
Looked over the FAQ site and was still having trouble figuring out exactly
what I'm doing wrong. So here's the code I have to capture the image and
then to save it:
(BalanceOff is a public Bitmap declared at the beginning.

Graphics currentTab = this.CreateGraphics();
Size s = this.Size;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, currentTab);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = currentTab.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
this.ClientRectangle.Height, dc1, 0, 0, 13369376);
BalanceOff = memoryImage;
BalanceOff.Save("sBalanceOff.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
currentTab.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);

Then BalanceOff.bmp only contains a black rectangle (the size of the form)
when I open it up.

Thanks again,
Melanie
 
Back
Top