Hi Richard,
From your description, you're wondering how to create a metafile image in
ASP.NET application.
Generally, this is GDI+ related question and the way we create a metafile
image is the same no matter in ASP.NET or WINFORM application. From the
code you provided, you problem is that how to get a hdc on the fly in
ASP.NET since there is no Winform control(which has can create a graphic)
in asp.net, yes?
I think you can create Bitmap instance and create a Graphics object using
the Graphics.FromImage(bitmap)
Here are some tech articles on dealing with GDI+ and graphics in ASP.NET :
#Generating graphics on the fly
http://www.aspheute.com/english/20000728.asp
#Hangman: Using GDI+ in ASP.NET Applications
http://www.c-sharpcorner.com/Graphics/CSHangManASPNETJOD.asp
And the following Books are very useful for GDI+ programming in .NET:
GDI+ Programming with C#
Programming Windows with C#
Then, after we can get a graphic object, the sequential works are all the
same with those in winform applcation. Do you think so? And below is a demo
page on creating and display a metafile I've tested on my side
=================aspx page=================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CreateMetaFile</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:Button id="btnCreate" runat="server" Text="Create
Metafile"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="btnDisplay" runat="server" Text="Display
Metafile"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>
=============code behind page class==========:
public class CreateMetaFile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDisplay;
protected System.Web.UI.WebControls.Button btnCreate;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCreate_Click(object sender, System.EventArgs e)
{
Bitmap bmp;
Graphics g = null;
Metafile mf = null;
try
{
bmp = new Bitmap(300,300,PixelFormat.Format32bppArgb);
g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
mf = new System.Drawing.Imaging.Metafile(Server.MapPath("./test.emf"),
hdc);
g.ReleaseHdc(hdc);
g.Dispose();
g = Graphics.FromImage(mf);
g.FillRectangle(new SolidBrush(Color.Yellow),0,0,300,300);
g.DrawEllipse(Pens.Blue, 40, 40, 220, 220);
g.Dispose();
}
catch(Exception ex)
{
Response.Write("<br>" + ex.Message);
}
finally
{
if(g != null)
{
g.Dispose();
}
if(mf != null)
{
mf.Dispose();
}
}
}
private void btnDisplay_Click(object sender, System.EventArgs e)
{
try
{
Bitmap bmp = new Bitmap(Server.MapPath("./test.emf"));
// Graphics g = Graphics.FromImage(bmp);
Response.Clear();
Response.ContentType ="Image/Gif";
bmp.Save(Response.OutputStream,ImageFormat.Gif);
Response.End();
bmp.Dispose();
}
catch(Exception ex)
{
Response.Write("<br>" + ex.Message);
}
}
}
=============================================
# Notice that since you'd like to create metafile in asp.net webfolder,
remember to grant the read write and create permission of the certain
folder to the Machine\ASPNET account(default process account)
Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx