Some common applications of GDI + under. NET (watermark, text, and rounded corner processing)

Source: Internet
Author: User

 

In some cases, you want to process images, such
Add General text to the image:
Source image

Program Processing

Add a watermark to the image:
Source image

Program Processing

There are also rounded corners:
Source image

Program Processing

Note:
(1) The following programs may not work for this application, but the basic principles are similar.
(2) I think it is not a violation of the Authority for testing.
Program interface:

Click here to download the program.
To improve the image quality
Image. Save (sDstFilePath, ImageFormat. Jpeg );
Change

ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
MyImageCodecInfo = ImageCodecInfo. GetImageEncoders () [0];
MyEncoder = Encoder. Quality;
MyEncoderParameters = new EncoderParameters (1 );
MyEncoderParameter = new EncoderParameter (myEncoder, 100L); // 0 to 100
MyEncoderParameters. Param [0] = myEncoderParameter;
Image. Save (sDstFilePath, myImageCodecInfo, myEncoderParameters );
MyEncoderParameter. Dispose ();
MyEncoderParameters. Dispose ();

This is the result of Improving the Quality:

The following code is provided:

Public class MyGDI
{
Public static void CreateWatermark (string sSrcFilePath, string sDstFilePath, string sText1, string sColor1, string sSize1, string sFont1, string sText2, string sColor2, string sSize2, string sFont2, string sBgColor, string sTransparence)
{
System. Drawing. Image image = System. Drawing. Image. FromFile (sSrcFilePath );
Graphics g = Graphics. FromImage (image );
G. SmoothingMode = SmoothingMode. AntiAlias;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. CompositingQuality = CompositingQuality. HighQuality;
G. TextRenderingHint = System. Drawing. Text. TextRenderingHint. AntiAlias; // anti-aliasing of Text
G. DrawImage (image, 0, 0, image. Width, image. Height );
Font f1 = new Font (sFont1, float. Parse (sSize1 ));
Font f2 = new Font (sFont2, float. Parse (sSize2 ));
Brush brushfortext1 = new SolidBrush (ColorTranslator. FromHtml (sColor1 ));
Brush brushfortext2 = new SolidBrush (ColorTranslator. FromHtml (sColor2 ));
Brush brushforbg = new SolidBrush (Color. FromArgb (Convert. ToInt16 (255 * float. Parse (sTransparence), ColorTranslator. FromHtml (sBgColor )));
G. RotateTransform (-20 );
Rectangle rect = new Rectangle (-image. Width/2-50, image. Height-50, image. Width * 2, 40 );
G. DrawRectangle (new Pen (brushforbg), rect );
G. FillRectangle (brushforbg, rect );
Rectangle rectfortext1 = new Rectangle (-image. Width/2 + image. Width/5, image. Height-45, image. Width * 2, 60 );
For (int I = 0; I <10; I ++)
G. DrawString (sText1, f1, brushfortext1, rectfortext1 );
Rectangle rectfortext2 = new Rectangle (-image. Width/2 + image. Width/5, image. Height-25, image. Width * 2, 60 );
For (int I = 0; I <10; I ++)
G. DrawString (sText2, f2, brushfortext2, rectfortext2 );
Image. Save (sDstFilePath, ImageFormat. Jpeg );
Image. Dispose ();
}
Public static void CreateRoundedCorner (string sSrcFilePath, string sDstFilePath, string sCornerLocation)
{
System. Drawing. Image image = System. Drawing. Image. FromFile (sSrcFilePath );
Graphics g = Graphics. FromImage (image );
G. SmoothingMode = SmoothingMode. HighQuality;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. CompositingQuality = CompositingQuality. HighQuality;
Rectangle rect = new Rectangle (0, 0, image. Width, image. Height );
GraphicsPath rectPath = CreateRoundRectanglePath (rect, image. Width/10, sCornerLocation); // construct the external path of the rounded corner
Brush B = new SolidBrush (Color. White); // rounded corner Background White
G. DrawPath (new Pen (B), rectPath );
G. FillPath (B, rectPath );
G. Dispose ();
Image. Save (sDstFilePath, ImageFormat. Jpeg );
Image. Dispose ();
}
Public static void CreatePlainText (string sSrcFilePath, string sDstFilePath, string sText, string sColor, string sSize, string sFont)
{
System. Drawing. Image image = System. Drawing. Image. FromFile (sSrcFilePath );
Graphics g = Graphics. FromImage (image );
G. SmoothingMode = SmoothingMode. AntiAlias;
G. InterpolationMode = InterpolationMode. HighQualityBicubic;
G. CompositingQuality = CompositingQuality. HighQuality;
G. DrawImage (image, 0, 0, image. Width, image. Height );
G. TextRenderingHint = System. Drawing. Text. TextRenderingHint. AntiAlias; // anti-aliasing of Text
Font f = new Font (sFont, float. Parse (sSize ));
Brush B = new SolidBrush (ColorTranslator. FromHtml (sColor ));
Rectangle rect = new Rectangle (10, 5, image. Width, image. Height); // open a distance appropriately
For (int I = 0; I <30; I ++) // enhance the brightness
G. DrawString (sText, f, B, rect );
Image. Save (sDstFilePath, ImageFormat. Jpeg );
Image. Dispose ();
}
Private static GraphicsPath CreateRoundRectanglePath (Rectangle rect, int radius, string sPosition)
{
GraphicsPath rectPath = new GraphicsPath ();
Switch (sPosition)
{
Case "TopLeft ":
{
RectPath. AddArc (rect. Left, rect. Top, radius * 2, radius * 2,180, 90 );
RectPath. AddLine (rect. Left, rect. Top, rect. Left, rect. Top + radius );
Break;
}
Case "TopRight ":
{
RectPath. AddArc (rect. Right-radius * 2, rect. Top, radius * 2, radius * 2,270, 90 );
RectPath. AddLine (rect. Right, rect. Top, rect. Right-radius, rect. Top );
Break;
}
Case "BottomLeft ":
{
RectPath. AddArc (rect. Left, rect. Bottom-radius * 2, radius * 2, radius * 2, 90, 90 );
RectPath. AddLine (rect. Left, rect. Bottom-radius, rect. Left, rect. Bottom );
Break;
}
Case "BottomRight ":
{
RectPath. AddArc (rect. Right-radius * 2, rect. Bottom-radius * 2, radius * 2, radius * 2, 0, 90 );
RectPath. AddLine (rect. Right-radius, rect. Bottom, rect. Right, rect. Bottom );
Break;
}
}
Return rectPath;
}
}

Conversion from: some common applications of GDI + under. NET (watermark, text, and rounded corner processing)

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.