|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis example code allows you to create rotated vertical text images in an ASP.NET web page using a custom Http Handler and the .NET Graphics libraries - GDI+. BackgroundIn a recent project I was asked to display a table of data like the one above with rotated vertical text column headings. For a tabular report that may have many columns, vertical text is sometimes the only way a user can view the data. Internet Explorer offers a way to display vertical text using style="writing-mode:tb-rl" but no other browsers support it and I wanted a solution that would work in Firefox and other browsers. Creating Vertical Text ImagesThe ImageCreationLibrary.cs file is in charge of creating and saving vertical text images in the format we want using the Microsoft's .NET GDI+ features.
The strategy that worked for me was to use The main code in ImageCreationLibrary.cs is inside the public string CreateVerticalTextImage(string stText, Font f)
{
string stFileName = Toolbox.GetMD5String(stText);
string stFullFilePath = this._baseImagePath + stFileName + ".jpg";
if (!File.Exists(stFullFilePath))
{
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
SizeF imageSize = _graphics.MeasureString(stText, f, 25, stringFormat);
System.Drawing.Image i = (Image)new Bitmap((int)imageSize.Width, (int)imageSize.Height);
Graphics g = Graphics.FromImage(i);
g.FillRectangle(Brushes.White, 0, 0, i.Width, i.Height);
//flip the image 180 degrees
g.TranslateTransform(i.Width, i.Height);
g.RotateTransform(180.0F);
g.DrawString(stText, f, Brushes.Black, 0, 0, stringFormat);
i.Save(stFullFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
return stFullFilePath;
}
Using the CodeWell that's all very nice and snazzy, but how do I use this code to actually include a vertical text image in my web page? I've created a custom HttpHandler: ImageViewer.cs, the ImageViewer class extends from IHttpHandler and within the //get the text passed in
string stText = context.Request.QueryString["text"];
if (stText != null && stText != "")
{
string stFileName = this.imageLib.CreateVerticalTextImage(stText, new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 12));
this.WriteImageFile(context, stFileName);
}
The Make sure to register the custom HttpHandler in the Web.Config file so we ensure requests to <system.web>
<httphandlers>
Finally, to include our image in a web page we need to UrlEncode the text we want on our image and pass it in the querystring to string stGET = System.Web.HttpUtility.UrlEncode(stText);
string stImageTag = "<img src=\"get_image.aspx?text=" + stGET + "\"/>";
Points of InterestWhen working with the GDI+ and matrix transformations you learn (or remember) much from Linear Algebra, it helps to use that F1 key within Visual Studio on methods your using such as TranslateTransform and RotateTransform and read the docs and examples and experiment with it. One thing to keep in mind: According to the docs these methods work by prepending the specified translation or rotation to the transformation matrix, so in my example the rotation is actually done first and then the translation is performed. ReferencesI was assited by the many great references I found (some from CodeProject) when trying to solve this problem: Many thanks to Horia Tudosie's Vertical Labels in Web Pages article which is very complete and helped me figure out some of the GDI+ code for rendering the text properly, I decided to share my code because I wanted a simple example from a slightly different perspective that also saved the created files so that it only has to generate the images once. Another good vertical text GDI+ article I found on codeproject (VB): Vertical Label Control in VB.NET History05/23/2007 - Created Article
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||