1 //------------------------------------------------------------------------------
2 // <copyright file="ImageCreator.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.Design.MobileControls.Util
8 {
9     using System;
10     using System.ComponentModel;
11     using System.Diagnostics;
12     using System.Drawing;
13     using System.IO;
14 
15     [
16         System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand,
17         Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
18     ]
19     [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
20     internal sealed class ImageCreator
21     {
22         const String _fontFamily = "Tahoma"; // default font used for the
23                                              // title and error message
24 
GetHeight( String text, Font font, int width )25         private static int GetHeight(
26             String text,
27             Font font,
28             int width
29         ) {
30             // THIS----S: I need a bitmap to get a graphics object to measure
31             // the string, but I can not create the bitmap I intend to return
32             // until I know how big it needs to be...
33 
34             using(Bitmap bmp = new Bitmap(1,1))
35             {
36             using(Graphics g = Graphics.FromImage(bmp))
37             {
38                 SizeF size = new SizeF(width, 0);
39                 size = g.MeasureString(text, font, size);
40                 return (int) (size.Height + 1);
41             }} // using bmp, g
42         }
43 
CreateBackgroundImage( ref TemporaryBitmapFile bmpFile, String controlID, String title, String message, bool infoMode, int controlWidth )44         internal static void CreateBackgroundImage(
45             ref TemporaryBitmapFile bmpFile,
46             String controlID,
47             String title,
48             String message,
49             bool infoMode,
50             int controlWidth
51         ) {
52             // Really, anything this small is not practically going to
53             // show readable text.  Truncate instead of trying to display
54             // the string vertically.
55             if(controlWidth < 75)
56             {
57                 controlWidth = 75;
58             }
59 
60             Bitmap errorIcon = infoMode? GenericUI.InfoIcon : GenericUI.ErrorIcon;
61 
62             bool showMessage = message != null && message.Length != 0;
63             bool showTitle = (title != null && title.Length != 0)
64                 || (controlID != null && controlID.Length != 0);
65 
66             Debug.Assert(showMessage || showTitle);
67 
68             //
69 
70 
71             using(
72                 Font normalFont = new Font(_fontFamily, 8, FontStyle.Regular),
73                 boldFont = new Font(normalFont.FontFamily, 8, FontStyle.Bold)
74             ) {
75             using(
76                 Brush controlTextBrush = new SolidBrush(SystemColors.ControlText),
77                 controlDarkBrush = new SolidBrush(SystemColors.ControlDark),
78                 controlBrush = new SolidBrush(SystemColors.Control),
79                 windowBrush = new SolidBrush(SystemColors.Window)
80             ) {
81             using(
82                 Pen controlDarkPen = new Pen(SystemColors.ControlDark),
83                 windowPen = new Pen(SystemColors.Window)
84             ) {
85                 int barHeight = 0;
86                 if(showTitle)
87                 {
88                     // We do not measure the height of the real title because
89                     // we inted to truncate rather than wrap.
90                     barHeight = GetHeight(
91                         "'",
92                         normalFont,
93                         (controlWidth - 30)
94                     ) + 6;
95                 }
96                 int messageHeight = 0;
97                 if(showMessage)
98                 {
99                     int textHeight = GetHeight(
100                         message,
101                         normalFont,
102                         (controlWidth - 30)
103                     );
104                     messageHeight = (textHeight < (errorIcon.Height + 6)) ?
105                         (errorIcon.Height + 6) : textHeight + 3;
106                 }
107 
108                 int width = 500; // normally only 300px visible.
109                 int height = barHeight + messageHeight;
110 
111                 Bitmap bitmap = new Bitmap(width, height);
112                 using(Graphics g = Graphics.FromImage(bitmap))
113                 {
114                     if (showTitle)
115                     {
116                         // The rectangle area
117                         g.FillRectangle(controlBrush, 0, 0, width, barHeight);
118                         // The gray line below the controlID
119                         g.DrawLine(controlDarkPen, 0, barHeight - 1, width, barHeight - 1);
120                         // Draw the text "controlTypeName - controlID"
121                         g.DrawString(controlID, boldFont, controlTextBrush, 2, 2);
122                         if(title != null && title.Length > 0)
123                         {
124                             int strPelLen = (int) g.MeasureString(controlID, boldFont).Width;
125                             g.DrawString(" - " + title, normalFont, controlTextBrush, 4 + strPelLen, 2);
126                         }
127                     }
128 
129                     if (showMessage)
130                     {
131                         // The transparent line between controlID and errormessage.
132                         g.DrawLine(windowPen, 0, barHeight, width, barHeight);
133                         // The message rectangle area
134                         g.FillRectangle(controlDarkBrush, 0, barHeight + 1, width, messageHeight - 1);
135                         // Draw the message text
136                         g.DrawString(message, normalFont, windowBrush,
137                             new RectangleF(20, barHeight + 1, controlWidth - 30, messageHeight - 1));
138                         // Draw the icon
139                         g.DrawImage(errorIcon, 2, barHeight + 3);
140                     }
141 
142                     if(bmpFile == null)
143                     {
144                         bmpFile = new TemporaryBitmapFile(bitmap);
145                     }
146                     else
147                     {
148                         bmpFile.UnderlyingBitmap = bitmap;
149                     }
150                 } // using g
151             }}} // using Fonts, Brushes, and Pens
152         }
153     }
154 }
155