public enum DynamicImageFormat {Gif = 0, Jpeg, Bmp, Png} * This code was highlighted with Source Code Highlighter .
public partial class DynamicImageControl: System.Web.UI.UserControl { public Bitmap DynamicBitmap { get ; set ; } public DynamicImageHandler.DynamicImageFormat DynamicImageFormat = DynamicImageHandler.DynamicImageFormat.Png; protected void Page_Load ( object sender, EventArgs e) { if (DynamicBitmap! = null ) { Guid _bitmapGuid = Guid .NewGuid (); Cache [_bitmapGuid.ToString ()] = DynamicBitmap; img.ImageUrl = String .Format ( "DynamicImageHandler.ashx? bitmap = {0} & format = {1}" , _bitmapGuid, ( int ) DynamicImageFormat); }}} * This code was highlighted with Source Code Highlighter .
Using Cache is not the best solution. I would be happy to hear alternatives.
public void ProcessRequest ( HttpContext context) { string _bitmapGuid = context.Request.QueryString [ "bitmap" ]; if (! String .IsNullOrEmpty (_bitmapGuid)) { if (context.Cache [_bitmapGuid]! = null ) { Bitmap _bitmap = ( Bitmap ) context.Cache [_bitmapGuid]; if (_bitmap! = null ) {ImageFormat DynImageFormat = GetImgFormat (context); MemoryStream MemStream = new MemoryStream (); _bitmap.Save (MemStream, DynImageFormat); MemStream.WriteTo (context.Response.OutputStream); MemStream.Dispose (); _bitmap.Dispose (); context.Cache.Remove (_bitmapGuid); }}}} * This source code was highlighted with Source Code Highlighter .
private ImageFormat GetImgFormat ( HttpContext context) {ImageFormat result = ImageFormat.Png; if (! String .IsNullOrEmpty (context.Request.QueryString [ "format" ])) {DynamicImageFormat _imgFormat = (DynamicImageFormat) Convert .ToInt32 (context.Request.QueryString [ "format" ]); switch (_imgFormat) { case DynamicImageFormat.Gif: result = ImageFormat.Gif; break ; case DynamicImageFormat.Jpeg: result = ImageFormat.Jpeg; break ; case DynamicImageFormat.Bmp: result = ImageFormat.Bmp; break ; case DynamicImageFormat.Png: result = ImageFormat.Png; break ; default : result = ImageFormat.Png; break ; }} return result; } * This source code was highlighted with Source Code Highlighter .
public bool IsReusable { get { return true ; }} * This source code was highlighted with Source Code Highlighter .
< form id = "form1" runat = "server" >
< asp: Label ID = "Label2" runat = "server" Text = "Dynamic creation of images:„ > </ asp: Label >
< uc1: DynamicImageControl ID = “DynamicImageControl1” runat = "server" />
< asp: Label ID = "Label1" runat = "server" Text = "End." > </ asp: Label >
</ form > This source code was highlighted with Source Code Highlighter .
public static string GetRandomPassword ( int length) { Random rand = new Random (); StringBuilder password = new System.Text. StringBuilder (length); for ( int i = 1; i <= length; i ++) { int charIndex; do {charIndex = rand.Next (48, 123); } while (! ((charIndex> = 48 && charIndex <= 57) || (charIndex> = 65 && charIndex <= 90) || (charIndex> = 97 && charIndex <= 122))); password.Append ( Convert .ToChar (charIndex)); } return password.ToString (); } public Bitmap DrawPassword () { Bitmap _bitmap = new Bitmap (150, 30); Graphics _graphics = Graphics .FromImage (_bitmap); SolidBrush _foreColor = new SolidBrush (Color.White); SolidBrush _backColor = new SolidBrush (Color.FromArgb (0x73, 0x82, 0x9F)); _graphics.FillRectangle (_backColor, 0, 0, 150, 30); Font _font = new Font ( "Verdana" , 15); Point _point = new Point (5, 5); string _password = GetRandomPassword (10); _graphics.DrawString (_password, _font, _foreColor, _point); if (_graphics! = null ) _graphics.Dispose (); return _bitmap; } * This source code was highlighted with Source Code Highlighter .
protected void Page_Load ( object sender, EventArgs e) {DynamicImageControl1.DynamicBitmap = DrawPassword (); DynamicImageControl1.DynamicImageFormat = DynamicImageHandler.DynamicImageFormat.Gif; } * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/25606/
All Articles