Introduction to ASP. NET Image Generation

Source: Internet
Author: User

ASP. NET Image Generation has the built-in ImageResizeTransform class, which allows you to adjust the Image size.

You can also extend ImageTransform to implement your own image conversion class.

The following code uses the ASP. NET Image Generation watermark:

 
 
  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Web;  
  5. usingMicrosoft.Web;  
  6. usingSystem.Collections.Specialized;  
  7. usingSystem.Data.SqlClient;  
  8. usingSystem.Drawing;  
  9.  
  10. namespaceNET35Lab.GeneratedImage.Web  
  11. {  
  12.  
  13. publicclassImageHandler1:ImageHandler  
  14. {  
  15.  
  16. publicImageHandler1()  
  17. {  
  18. this.ImageTransforms.Add(newImageResizeTransform
    {Width=320,Mode=ImageResizeMode.Fit});  
  19. this.ImageTransforms.Add(newCopyrightTransform{Text="Guushuuse.NET"});  
  20. this.EnableClientCache=true;  
  21. this.EnableServerCache=true;  
  22.  
  23. }  
  24.  
  25. publicoverrideImageInfoGenerateImage(NameValueCollectionparameters)  
  26. {  
  27.  
  28. intintimageID=int.Parse(parameters["imageid"]);  
  29.  
  30. SqlConnectionconnection=newSqlConnection(  
  31. @"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.
    mdf;IntegratedSecurity=True;UserInstance=True;");  
  32.  
  33. connection.Open();  
  34.  
  35. SqlCommandcommand=newSqlCommand
    ("selectimage_datafromt_imageswhereimage_id="+imageID,connection);  
  36.  
  37. SqlDataReaderdr=command.ExecuteReader();  
  38.  
  39. dr.Read();  
  40.  
  41. returnnewImageInfo((Byte[])dr[0]);  
  42.  
  43. }  
  44. }  
  45.  
  46. publicclassCopyrightTransform:ImageTransform  
  47. {  
  48. privateconstintVERTICAL_PADDING=5;  
  49. privateconstintHORIZONAL_PADDING=5;  
  50.  
  51. publicstringText{get;set;}  
  52. publicFontFont{get;set;}  
  53. publicColorFontColor{get;set;}  
  54.  
  55. publicCopyrightTransform()  
  56. {  
  57. //AssignDefaults  
  58. FontColor=Color.FromArgb(128,255,0,0);  
  59. Font=newFont("Courier",13);  
  60. }  
  61.  
  62.  
  63. publicoverrideSystem.Drawing.ImageProcessImage(System.Drawing.Imageimg)  
  64. {  
  65. GraphicsGraphicsgra=Graphics.FromImage(img);  
  66. SizeFsz=gra.MeasureString(Text,Font);  
  67. gra.DrawString(Text,Font,newSolidBrush(FontColor),
    img.Width-sz.Width-HORIZONAL_PADDING,img.Height-sz.Height-VERTICAL_PADDING);  
  68. returnimg;  
  69. }  
  70.  
  71. publicoverridestringUniqueString  
  72. {  
  73. get  
  74. {  
  75. returnbase.UniqueString+Text+FontColor.ToString()+Font.ToString();  
  76. }  
  77. }  
  78. }  

The above describes ASP. NET Image Generation.

  1. XML data displayed on the ASP. NET page
  2. What is iframe asp. NET?
  3. ASP. NET lifecycle display
  4. Solve the Problem of ASP. net ajax script errors
  5. ASP. NET Applications

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.