silterra
asked on
Drawing image and add hyperlink into the image
I have a program to draw an image, and within the image itself I need to add some hyperlink into it. For example in the below codes, X, Y & Z will open a new window and link to a file share site, i.e: X link to //fileshare/folder1, Y link to //fileshare/folder2, and Z will link to //fileshare/folder3. My problem here is how to link the X, Y & Z to the related site.
Bitmap objBitmap;
Graphics objGraphics;
int XLen = 500;
int YLen = 500;
int factor = 27;
objBitmap = new Bitmap(500, 500);
objGraphics = Graphics.FromImage(objBitm ap);
objGraphics.Clear(Color.Wh ite);
Pen p = new Pen(Color.Yellow, 0);
int XRectLen = 520;
int YRectLen = 520;
Rectangle rect = new Rectangle(0, 0, XRectLen, YRectLen);
Rectangle rect1 = new Rectangle(0, 0, 500, 500);
SolidBrush KhakiBrush = new SolidBrush(Color.Khaki);
objGraphics.FillEllipse(Kh akiBrush, rect1);
FontFamily fontfml = new FontFamily(GenericFontFami lies.Serif );
Font font = new Font(fontfml, 10);
SolidBrush brush = new SolidBrush(Color.Blue);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.Directio nVertical;
objGraphics.DrawString("X" , font, brush, 340, 360);
objGraphics.DrawString("Y" , font, brush, 230, 200);
objGraphics.DrawString("Z" , font, brush, 210, 380);
objBitmap.Save(Response.Ou tputStream , ImageFormat.Gif);
objBitmap.Save(Server.MapP ath("x.jpg "), ImageFormat.Jpeg);
objBitmap.Dispose();
objGraphics.Dispose();
Please help. Thanks.
Bitmap objBitmap;
Graphics objGraphics;
int XLen = 500;
int YLen = 500;
int factor = 27;
objBitmap = new Bitmap(500, 500);
objGraphics = Graphics.FromImage(objBitm
objGraphics.Clear(Color.Wh
Pen p = new Pen(Color.Yellow, 0);
int XRectLen = 520;
int YRectLen = 520;
Rectangle rect = new Rectangle(0, 0, XRectLen, YRectLen);
Rectangle rect1 = new Rectangle(0, 0, 500, 500);
SolidBrush KhakiBrush = new SolidBrush(Color.Khaki);
objGraphics.FillEllipse(Kh
FontFamily fontfml = new FontFamily(GenericFontFami
Font font = new Font(fontfml, 10);
SolidBrush brush = new SolidBrush(Color.Blue);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.Directio
objGraphics.DrawString("X"
objGraphics.DrawString("Y"
objGraphics.DrawString("Z"
objBitmap.Save(Response.Ou
objBitmap.Save(Server.MapP
objBitmap.Dispose();
objGraphics.Dispose();
Please help. Thanks.
ASKER
Hi ozymandias,
Thanks for your feedback, let me try it out.
Thanks for your feedback, let me try it out.
ASKER
Hi ozymandias,
Thanks a lot, it's work!!!
But I have a message displayed [object] in the main window after I clicked on the hyperlink, where the hyperlink will open a new browser window. I had changed the code below
area.Attributes.Add("href" ,"#")
to
area.Attributes.Add("href" , "javascript:window.open('t estX.aspx' ,'Window1' ,'height=5 50, width=550,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,men ubar=no ');");
Advenced thanks for your help.
Thanks a lot, it's work!!!
But I have a message displayed [object] in the main window after I clicked on the hyperlink, where the hyperlink will open a new browser window. I had changed the code below
area.Attributes.Add("href"
to
area.Attributes.Add("href"
Advenced thanks for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to create an Image Map from the image so that when the image is displayed to a user on a web page they can click on the areas in the ikmage that contain the text and go to the hyperlinks.
Here is an example that incorporates your code :
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
namespace WebImageMap
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.
myImage.ImageUrl = "x.jpg";
myImage.Attributes.Add("us
this.Controls.Add(myImage)
HtmlGenericControl map = new HtmlGenericControl("map");
map.Attributes.Add("name",
Rectangle[] rects = this.MakeImage();
foreach(Rectangle rect in rects){
HtmlGenericControl area = new HtmlGenericControl("area")
area.Attributes.Add("shape
area.Attributes.Add("coord
area.Attributes.Add("href"
map.Controls.Add(area);
}
this.Controls.Add(map);
}
private Rectangle[] MakeImage(){
Bitmap objBitmap;
Graphics objGraphics;
int XLen = 500;
int YLen = 500;
int factor = 27;
Rectangle r1;
Rectangle r2;
Rectangle r3;
objBitmap = new Bitmap(500, 500);
objGraphics = Graphics.FromImage(objBitm
objGraphics.Clear(Color.Wh
Pen p = new Pen(Color.Yellow, 0);
int XRectLen = 520;
int YRectLen = 520;
Rectangle rect = new Rectangle(0, 0, XRectLen, YRectLen);
Rectangle rect1 = new Rectangle(0, 0, 500, 500);
SolidBrush KhakiBrush = new SolidBrush(Color.Khaki);
objGraphics.FillEllipse(Kh
FontFamily fontfml = new FontFamily(GenericFontFami
Font font = new Font(fontfml, 10);
SolidBrush brush = new SolidBrush(Color.Blue);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.Directio
objGraphics.DrawString("X"
SizeF textsize = objGraphics.MeasureString(
r1 = new Rectangle(340, 360, (int)textsize.Width, (int)textsize.Height);
textsize = objGraphics.MeasureString(
objGraphics.DrawString("Y"
r2 = new Rectangle(230, 200, (int)textsize.Width, (int)textsize.Height);
textsize = objGraphics.MeasureString(
objGraphics.DrawString("Z"
r3 = new Rectangle(210, 380, (int)textsize.Width, (int)textsize.Height);
//objBitmap.Save(Response.
objBitmap.Save(Server.MapP
objBitmap.Dispose();
objGraphics.Dispose();
return new Rectangle[]{r1, r2, r3};
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
}
#endregion
}
}