开发者

Get click event for Graphics in asp.net

开发者 https://www.devze.com 2023-04-10 11:59 出处:网络
We are writinga text in an image using System.Drawing.Graphics.Drawstring. Below code is shown Bitmap bitMapImage = new System.Drawing.Bitmap(@\"D:\\ABC\\Chart1.png\");

We are writing a text in an image using System.Drawing.Graphics.Drawstring. Below code is shown

        Bitmap bitMapImage = new System.Drawing.Bitmap(@"D:\ABC\Chart1.png");
        Graphics graphicImage = Graphics.FromImage(bitMapImage);
        //Smooth graphics is nice.
        graphicImage.SmoothingMode = SmoothingMode.AntiAlias;

        String drawString = "250";
        Font drawFont = new Font("Arial", 12,FontS开发者_运维技巧tyle.Bold);
        SolidBrush drawBrush = new SolidBrush(Color.White);
        PointF drawPoint= new PointF(169.0F, 85.0F); .

        graphicImage.DrawString(drawString, drawFont, drawBrush, drawPoint);

        //Set the content type
        Response.ContentType = "image/jpeg";

        //Save the new image to the response output stream.
        bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);

        //Clean house.
        graphicImage.Dispose();
        bitMapImage.Dispose();

The code takes an imagefile and writes the string (here:250).Now when user clicks 250, the new window should get open.

Not sure how to get the click event of 250?

Please help! Thanks


This is ASP.NET detecting a click on drawn text in an image is possible but far from ideal. You should instead render text using a div or other html element by positioning it over the image and detect clicks on the that instead using javascript.

See Text Blocks Over Image


It would be easier to render the image to a container like a DIV or SPAN, and add an onclick to container:

<div onclick="doSomething();">
    <!-- rendered image here -->
</div>

You might also look into using an Image Map, like this:

function foo() {
 alert("Hello World!")
}

<img src="/images/someimage.png" usemap="#somemap" />
<map name="somemap">
    <area coords="0,0,82,126" onclick="foo()" nohref="true" href="#" />
</map>
0

精彩评论

暂无评论...
验证码 换一张
取 消