Saving the content of a control as a Bitmap

petrosken

New member
Joined
Jun 13, 2003
Messages
4
Location
Milano - Italy
Hi guys.
Anybody can help me with this problem?

I would like to save the content of a control (for example a TreeView) as a Bitmap.

I think I must go throug HWND and such things. I made a lot of attempts but all unsuccessful.

What I really need is to export the complete content of a control EVEN if this has scrollbars and only a part of the content is visible.

Thanks a lot.
 
Oh, I read that topic and its another matter.
In that topic the user at last gets a Graphic from a bitmap, draws on the Graphic and then saves the bitmap.

I need to do another thing.

I use a "whatever" control then at a certain point I would like to do something like this

Graphics myGr = WhatherverControl.CreateGraphics();
Bitmap bmp = CreateABitmapFromAGraphics(myGr); :D
bmp.Save(bmpFileName);

Heres something that I already tried

[DllImport("gdi32.dll",
ExactSpelling=true,
SetLastError=true)]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hObject,
int width,
int height);

----------------------
Graphics g = treeChartView1.CreateGraphics();
IntPtr hdc = g.GetHdc();
IntPtr bmpHdc = CreateCompatibleBitmap(hdc,sizeX,sizeY);
Bitmap bmp = Bitmap.FromHbitmap(bmpHdc);

then saved the bitmap and got a black image
 
When you draw to a control, youre drawing directly to screen memory. Your bitmap isnt stored anywhere. Thats why there is no method to get a bitmap from a control or graphics object.

The best you could hope for would be to interop and see if you can get BitBlt working.
 
Sigh, I made this attempt also.

I post the code here, I hope I made some terrible mistake so that it can be fixed. Im losing the hope :(


----------


[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
public static extern Bool BitBlt(IntPtr hObject,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hObjSource,
int nXSrc,
int nYSrc,
int dwRop);

----


Graphics g = TreeView1.CreateGraphics();
IntPtr hdc = g.GetHdc();
Bitmap bmp = new Bitmap(630,570);
Graphics bg = Graphics.FromImage(bmp);
IntPtr bHdc = bg.GetHdc();

BitBlt(hdc,
0, 0,
630, 570,
bHdc,
0, 0,
0x00CC0020);

Bitmap bmp = Bitmap.FromHbitmap(bHdc);

bmp.Save("c:\\pippo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
Console.WriteLine("Salvato");
g.ReleaseHdc(hdc);


----


any ideas??
 
Back
Top