Obtaining Graphics Object

J

jediknight

Hi,

I am designing a web form which as an imagemap. I need to obtain a
graphics object so I can draw on this imagemap.

Can anyone tell me how to do this?

I have tried creating a bitmap object and doing Graphics.FromImage call
but the image on the imagemap disappears leaving a black background.


Many thanks in advance!!
 
M

Marina Levit [MVP]

I'm not sure how you intend to draw on a web page? You are talking about
the server generating a bunch of HTML to send to the client. The server
cannot draw something in the client browser drawing surface.

Perhaps I am misunderstanding what you are trying to do.
 
K

Kelly Herald

Do the following:

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(50, 100,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);


What you will most likely want to do is create a Generic Handler (*.ashx) to
do the drawing. If you use the ASHX approach then when you are done drawing
then just output the bitmap to the OutputStream.

bmp.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);

On your ASPX page the source URL for the image will be the path to the ASHX
file.

Sample code:

Image.ashx
using System;
using System.Web;


public class Image : IHttpHandler
{

public void ProcessRequest (HttpContext context)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(50, 100,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);

g.Clear(System.Drawing.Color.Blue);

bmp.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable
{
get
{
return false;
}
}

}

Default.aspx
<img src="Image.ashx" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top