473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3620
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Julian Ziersch | last post by:
This code causes a severe memory leak - the critical part is the call to GetHenhmetafile(). System.Drawing.Imaging.Metafile lImage = new System.Drawing.Imaging.Metafile("C:\\meta0.emf"); IntPtr...
0
by: Balu Ramachandran | last post by:
How can I create the Thumbnail view from the form? In the following approach I tried but Iams facing some limitations to get the thumbnail image of the form alone. Plaese any one can help in...
3
by: Richard Skopal | last post by:
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,...
2
by: George Yefchak | last post by:
Hi I need to retrieve a metafile image from the clipboard and render it as a bitmap in my application. In particular, an image will be placed on the clipboard by pasting from PowerPoint (I...
4
by: dzemo | last post by:
How to make advanced charts in vb.net which tool to use to draw charts using data from sql server 2000. Chart in Crystal reports are not good they don't have enough features for my use. Any better...
2
by: Tilo Pätzold | last post by:
Hi Everybody (especially Microsoft), we build EMF files with rotated text for export to office (powerpoint, word). It is planned that the text can be edited in the office document. Without the...
2
by: NickP | last post by:
Hi there, I am obtaining a meta file from the clipboard via the following code Dim CF_ENHMETAFILE As Integer = 14 Dim cMFeImage As Imaging.Metafile Dim pIPrClipboard As IntPtr =...
2
by: Laurent Navarro | last post by:
Hello, I am using a library which returns a byte containing RAW data, ie all pixels' color values coded in a byte array without header. I would like to save those data into a JPEG file so I...
2
by: Alexander Gorbylev | last post by:
Hi! Let the size of vector is e.g. 3.5". I render the same vector on a printer & a screen on the same procedure: printDoc_BeginPrint(object sender, PrintEventArgs e) { .... vector.Width *...
1
by: bern11 | last post by:
I can get bitmaps from the clipboard, but how do I get Metafiles? The specific instance I am testing is copying a piece of Word clip-art into the clipboard and trying to read it in an application. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.