因此,我有一個ashx文件,它可以創建一個寫有文本的圖像。將替代文本添加到以編程方式創建的圖像
//create the image for the string
Font f = new Font("Arial", 15F);
Graphics g = Graphics.FromImage(b);
SolidBrush whiteBrush = new SolidBrush(Color.FromArgb(0,71,133));
SolidBrush blackBrush = new SolidBrush(Color.White);
RectangleF canvas = new RectangleF(0, 0, 105, 45);
g.FillRectangle(whiteBrush, canvas);
context.Session["test"] = randomString;
g.DrawString(context.Session["test"].ToString(), f, blackBrush, canvas);
context.Response.ContentType = "image/gif";
b.Save(context.Response.OutputStream, ImageFormat.Gif);
當被調用時,它創建我想要的圖像,但意識到它沒有用於輔助功能的替代文本選項。
關於如何添加替代文字到圖像的任何想法?
在我的aspx頁面我有這樣的:
<asp:Image ID="myImage" class="styleimage" src="ImageMaker.ashx" runat="server"/>
我曾嘗試:
myImage.AlternateText = HttpContext.Current.Session["test"].ToString();
我收到NullReferenceException
。
這顯然發生的原因是Session["test"]
在頁面加載後得到填充(所以頁面加載,圖像被渲染,然後處理程序被調用)。
我該如何解決這個問題?
圖像文件不包含「alt」文本。從來沒有。你的意思是HTML圖像元素上的'alt'屬性? – Oded 2012-03-15 16:49:27
正確!很抱歉對於這個誤會。 – tray 2012-03-15 16:51:14
那是什麼與你發佈的圖像生成代碼有關?我在這裏看不到_any_ HTML。 – Oded 2012-03-15 16:51:50