Share C # dynamic text image generation solution

Source: Internet
Author: User

We all know that if we want to make the text on the Web page more dazzling, we can only use image processing software such as potoshop and firework to make the text into images, in this way, the browser type and font of the browser are not dependent. However, in our web applications, there are often dynamic texts, so we cannot use image processing software to process them.ProgramDynamic generation. Fortunately. net Framework provides us with convenience. Next we will use system. the bitmap class and graphics class in the drawing namespace are used to compile a class for generating text images. This class can meet the following requirements when generating images:
1. You can specify the text font, size, and color (Note: This font is required for the specified text on the Web server );
2. Add text shadow;
3. You can specify the text transparency;
4. You can specify the background image or background color;
5. You can specify the size (width and height) of the generated image );
6. You can specify the text position (left margin and top margin );
7. When the text font size you set is too large, the text size can be automatically adjusted to adapt to the size of the generated image.

Implementation of this classCodeAs follows:

Using system. drawing; using system. drawing. drawing2d; using system. drawing. imaging; namespace ycweb. controls. utility {/** // <summary> // watermark // </Summary> public class watermark {private int _ width; private int _ height; private string _ fontfamily; private int _ fontsize; private bool _ adaptable; private fontstyle _ fontstyle; private bool _ shadow; private string _ backgroundimage; private color _ bgcolor; private int _ left; private string _ resultimage; private string _ text; private int _ top; private int _ Alpha; private int _ Red; private int _ green; private int _ blue; private long _ quality; public watermark () {// todo: Add constructor logic here // _ width = 460; _ Height = 30; _ fontfamily = "文 "; _ fontsize = 20; _ fontstyle = fontstyle. regular; _ adaptable = true; _ shadow = false; _ left = 0; _ Top = 0; _ alpha = 255; _ Red = 0; _ Green = 0; _ Blue = 0; _ backgroundimage = ""; _ Quality = 100; _ bgcolor = color. fromargb (255,229,229,229);}/** // <summary> // font // </Summary> Public String fontfamily {set {This. _ fontfamily = value ;}}/ ** // <summary> // text size /// </Summary> Public int fontsize {set {This. _ fontsize = value; }}/ ** // <summary> // text style // </Summary> Public fontstyle {get {return _ fontstyle ;} set {_ fontstyle = value ;}} /** // <summary> // transparency 0-255,255 indicates opacity // </Summary> Public int Alpha {get {return _ Alpha ;} set {_ alpha = value ;}} /** // <summary> // specifies whether the watermark text uses shadow. // </Summary> Public bool shadow {get {return _ shadow ;} set {_ shadow = value ;}} public int red {get {return _ Red;} set {_ Red = value ;}} public int green {get {return _ green ;} set {_ Green = value ;}} public int blue {get {return _ blue ;}set {_ Blue = value ;}} /** // <summary> // basemap // </Summary> Public String backgroundimage {set {This. _ backgroundimage = value ;}}/** // <summary> // left margin of the watermark text /// </Summary> Public int left {set {This. _ left = value ;}/ ** // <summary> // top margin of the watermark text /// </Summary> Public int top {set {This. _ Top = value ;}/ ** // <summary> /// generated image /// </Summary> Public String resultimage {set {This. _ resultimage = value ;}}/ ** // <summary> // watermark text /// </Summary> Public String text {set {This. _ text = value ;}} /** //// <summary> /// width of the generated image /// </Summary> Public int width {get {return _ width ;} set {_ width = value ;}} /** //// <summary> /// height of the generated image /// </Summary> Public int height {get {return _ height ;} set {_ Height = value ;}}/ ** // <summary> // if the text is too large, adjust the text size based on the background image, the default value is adaptive // </Summary> Public bool adaptable {get {return _ adaptable;} set {_ adaptable = value ;}} public color bgcolor {get {return _ bgcolor;} set {_ bgcolor = value ;}}/ ** // <summary> // output image quality, quality range: 0-100; Type: Long // </Summary> Public long quality {get {return _ quality;} set {_ Quality = value ;}} /** // <summary> // generate a watermark immediately /// </Summary> // <returns> returns true if the watermark is generated successfully, otherwise, false </returns> Public bool create () {try {Bitmap bitmap; graphics g; // If (this. _ backgroundimage. trim () = "") {bitmap = new Bitmap (this. _ width, this. _ height, pixelformat. format64bppargb); G = graphics. fromimage (Bitmap); G. clear (this. _ bgcolor);} else {bitmap = new Bitmap (image. fromfile (this. _ backgroundimage); G = graphics. fromimage (Bitmap);} G. smoothingmode = smoothingmode. highquality; G. interpolationmode = interpolationmode. highqualitybicubic; G. compositingquality = compositingquality. highquality; font F = new font (_ fontfamily, _ fontsize, _ fontstyle); sizef size = G. measurestring (_ text, f); // adjust the text size until it can adapt to the image size while (_ adaptable = true & size. width> bitmap. width) {_ fontsize --; F = new font (_ fontfamily, _ fontsize, _ fontstyle); size = G. measurestring (_ text, f);} brush B = new solidbrush (color. fromargb (_ Alpha, _ Red, _ Green, _ blue); stringformat strformat = new stringformat (); strformat. alignment = stringalignment. near; If (this. _ shadow) {brush b2 = new solidbrush (color. fromargb (90, 0, 0, 0); G. drawstring (_ text, F, B2, _ left + 2, _ top + 1);} G. drawstring (_ text, F, B, new pointf (_ left, _ top), strformat); bitmap. save (this. _ resultimage, imageformat. JPEG); bitmap. dispose (); G. dispose (); Return true;} catch {return false ;}}}}

The call is quite simple. I will not repeat it here. I will take a picture of the effect I have used for your reference.

ArticleSource:Http://aspsir.cnblogs.com/archive/2006/04/30/389513.html

Existing0People comment, slam->Here<-Participate in the discussion

Iteye recommendation

    • -Software talents free of language and low guarantee paid study in the United States! -

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.