470,593 Members | 2,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,593 developers. It's quick & easy.

How to create metafile in ASP.NET?

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.
Nov 18 '05 #1
2 3445
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.

"Richard Skopal" <sk*****@trexima.cz> wrote in message
news:#w*************@tk2msftngp13.phx.gbl...
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.

Nov 18 '05 #2
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/Graphic...nASPNETJOD.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



Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Julian Ziersch | last post: by
reply views Thread by Balu Ramachandran | last post: by
3 posts views Thread by Richard Skopal | last post: by
2 posts views Thread by George Yefchak | last post: by
4 posts views Thread by dzemo | last post: by
2 posts views Thread by NickP | last post: by
2 posts views Thread by Laurent Navarro | last post: by
1 post views Thread by bern11 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.