locked
Saving "Graphics" to a file RRS feed

  • Question

  • User1651711768 posted
    I am trying to generate a "Bitmap" class from "Graphics" class. I know there is a way to do reverse, however I could not find anything that can be generate a file from "Graphics" object. Here is my need, I want to save the appearance of certain Windows Control to a file. Each Windows Control has a CreateGraphics method which returns Graphics object, is there a way to save it into a file? I am trying to convert "Graphics" to a "Bitmap" as Bitmap class has "Save" method that can write to a file, however if you know that there is other way, please respond. Thanks in advance.
    Monday, December 22, 2003 6:05 PM

All replies

  • User604330581 posted
    CreateGraphics doesn't really give you a graphical representation of the control. The Graphics object is an object you can use to draw to a bitmap or the screen - Control.CreateGraphics gives you a Graphics object you can use to draw on the control's surface. Many Windows controls are actually implemented and drawn by Windows, so they cannot be copied to a Graphics object using only System.Drawing. The most reliable way to capture the output of a form or control is to send it the Windows WM_PRINT message. This message instructs a window to draw itself into a Windows HDC, which you can get from a Graphics object. Here's a very simple example app, that puts up a form - when you click on the button, it captures the form and saves it in a file: using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Windows.Forms; public class WmpForm : Form { private Button btn; [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr hdc, int flags); public WmpForm() { btn = new Button(); btn.Text = "Print"; btn.Click += new EventHandler(this.OnClick); Controls.Add(btn); } public void OnClick(Object sender, EventArgs e) { Bitmap bmp = new Bitmap(Width, Height); Graphics g = Graphics.FromImage(bmp); IntPtr hdc = g.GetHdc(); try { SendMessage(this.Handle, 0x0317, hdc, 0x36); } finally { g.ReleaseHdc(hdc); } bmp.Save("foo.jpg", ImageFormat.Jpeg); } } public class Wmp { public static void Main(String[] args) { WmpForm form = new WmpForm(); Application.Run(form); } }
    Monday, January 5, 2004 1:42 AM
  • User1502633704 posted
    Is there anyway to send a WM_PRINT message to the browser purely using ASP.NET? That is, without creating a windows form? I'd like to put a crawler on my web server that goes to target sites and take homepage snapshots. But I can't load an EXE to the server. Any ideas?
    Monday, February 2, 2004 7:48 PM