How to create metafile in ASP.NET?

  • Thread starter Thread starter Richard Skopal
  • Start date Start date
R

Richard Skopal

In .NET Windows forms I can create a metafile using this code:

Graphics grph = aControl.CreateGraphics();
IntPtr ipHDC = grph.GetHdc();
Metafile mf = new Metafile(aImgFilePath, ipHDC, EmfType.EmfOnly);
grph.ReleaseHdc(ipHDC);
grph.Dispose();
grph = Graphics.FromImage(mf);
grph.DrawRectangle(aPen, 10, 10, 100, 120);
grph.Dispose();

But how to create metafile in ASP.NET, where are neither WinForms controls
nor any GetHdc() method?
Thanks.
 
What are you planning to do with a Metafile in an ASP.Net app, which works
with a browser that doesn't support metafiles?

In any case, you create the image in memory, rather than as a file.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
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
 
Hi Richard,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. 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
 
Back
Top