So I have an ashx file that creates an image with text written on it.
//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);
When called, it creates the image I want, but realized that it doesn’t have have an alt text option for accessibility purposes.
Any idea on how to add alternate text to an image?
In my aspx page I have this:
<asp:Image ID="myImage" class="styleimage" src="ImageMaker.ashx" runat="server"/>
I have tried:
myImage.AlternateText = HttpContext.Current.Session["test"].ToString();
I receive a NullReferenceException.
This apparently happens because Session["test"] gets populated after the page load ( so the page loads, the image gets rendered, then the handler gets called).
How do I solve this?
You can create the session variable in your page_load and assign the
randomStringto it there.You will then be able to access it in the handler to use in creating the image.
This way, you follow the timeline of the different events: