Save usercontrol as bitmap

  • Thread starter garethrichardadams
  • Start date
G

garethrichardadams

Hello all,

I've got a user control I've created called canvas. Doesn't do much at
the moment.

I've set the background image to a small bitmap using BackgroundImage
and I've set it to tiled using BackgroundImageLayout =
ImageLayout.Tiled.

It all works - I get a tiled image.

What I'd like to do now is save the tiled image (i.e. exactly what I
can see in the window) to disk. The closest I can get is to save the
non-tiled image by referring to BackgroundImage but this isn't much
help!!

Can anyone point me in the right direction? Thanks

gareth

Rectangle areaToSave = new Rectangle();
areaToSave = canCanvas.ClientRectangle;
Bitmap imageToSave = new Bitmap(areaToSave.Width, areaToSave.Height);
Graphics g = Graphics.FromImage(imageToSave);
g.DrawImage(canCanvas.BackgroundImage, areaToSave, areaToSave,
GraphicsUnit.Pixel);
imageToSave.Save(fileName, imageFormatToSave);
g.Dispose();
imageToSave.Dispose();
 
G

garethrichardadams

Nevermind, worked it out.

Bitmap imageToSave = new Bitmap(canCanvas.ClientRectangle.Width,
canCanvas.ClientRectangle.Height);

Graphics g = Graphics.FromImage(imageToSave);

PaintEventArgs pe = new PaintEventArgs(g, new Rectangle(0, 0,
canCanvas.ClientRectangle.Width, canCanvas.ClientRectangle.Height));

this.InvokePaintBackground(canCanvas, pe);
this.InvokePaint(canCanvas, pe);

imageToSave.Save(fileName, imageFormatToSave);

g.Dispose();
imageToSave.Dispose();
 

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