[Yugong Series] October 2023 .NET CORE tool case-DeveloperSharp (picture cropping, scaling, and watermarking)

About the author, Yu Gong moves the code
“Title”: Huawei Cloud Special Editor, Huawei Cloud Cloud Enjoyment Expert, Huawei Developer Expert, Huawei Product Cloud Testing Expert, CSDN Blog Expert, Alibaba Cloud Expert Blogger, Tencent Cloud Excellent Blogger, Nuggets Excellent Blogger, 51CTO Blogging experts and more.
“Recent Honors”: CSDN Blog Star TOP2 in 2022, Huawei Cloud Top Ten Bloggers in 2022, etc.
“Blog Content”: .NET, Java, Python, Go, Node, front-end, IOS, Android, Hongmeng, Linux, Internet of Things, network security, big data, artificial intelligence, U3D games, small programs and other related field knowledge.
Welcome Like?Comment?Collect

Article directory

  • Foreword
  • 1. Cropping, scaling and watermarking of pictures
    • 1. Installation package
    • 2.Use
      • 2.1 Original image cropping
      • 2.2 Thumbnail of original image
      • 2.3 Add text watermark to original image
      • 2.4 Add image watermark to original image
    • 3. Customize image cropping, scaling, and watermarking
  • Thank you: a letter to readers

Foreword

Cropping, scaling and watermarking images are common image processing operations. Here are some commonly used tools and libraries:

  1. PIL: Python Imaging Library, a standard library for image processing in Python, provides rich image processing functions.
  2. OpenCV: A popular open source computer vision library that supports a variety of image processing and computer vision tasks.
  3. ImageMagick: A powerful image processing tool that can be used to process images from the command line and supports various common formats, including JPEG, PNG, and GIF.
  4. GraphicsMagick: An image processing tool similar to ImageMagick, with higher performance and lower memory usage.
  5. Wand: A Python library based on ImageMagick that provides a simple interface to process images.

This article mainly introduces the use of DeveloperSharp. DeveloperSharp is a necessary system platform for R&D of medium and large projects, and it is also a low-code platform.

It mainly includes the following functions:

  • Database operations based on Sql statements, stored procedures, transactions, and paging. And supports almost all types of databases on the market.
  • Picture manipulation. Crop, scale, watermark.
  • http request call (Post and Get)
  • Efficient paging
  • Web service/WebApi load balancing
  • Database load balancing and read-write separation
  • CORS cross-domain access
  • UUID globally unique identifier
  • MQ message queue (please use DeveloperSharp.RabbitMQ package separately)
  • Redis cache (please use DeveloperSharp.Redis package separately)
  • Load balancing of “heterogeneous databases”
  • Other related functions

1. Cropping, scaling, and watermarking of images

1. Installation package

DeveloperSharp

2.Use

1. WEB project

using DeveloperSharp.Framework.CoreUtility; //Please reference the DeveloperSharp package from NuGet first
--------------------------

//First perform tool preloading in the Startup.cs or Program.cs file
Services.AddTransient<IUtility, Utility>();
--------------------------

//IU is an IUtility type object obtained through dependency injection in the relevant file.
//IU.PictureCutAsync("D:/fengjing.jpg", 500, 350, 200, 200, "D:/fj1.jpg");
//IU.PictureThumbnailAsync("D:/fengjing.jpg", 400, 300, "D:/fj2.jpg");
//IU.PictureTextWatermarkAsync("D:/fengjing.jpg", "Hello world!", new Font("华文新伟", 40, FontStyle.Bold), Brushes.Azure, 500, 50, "D: /fj3.jpg");
//IU.PictureImageWatermarkAsync("D:/fengjing.jpg","D:/log.png",680, 380, "D:/fj4.jpg");

2.Console project

using DeveloperSharp.Framework.CoreUtility; //Please reference the DeveloperSharp package from NuGet first
--------------------------

IUtility ui = new Utility();
//ui.PictureCut("D:/fengjing.jpg", 500, 350, 200, 200, "D:/fj1.jpg");
//ui.PictureThumbnail("D:/fengjing.jpg", 400, 300, "D:/fj2.jpg");
//ui.PictureTextWatermark("D:/fengjing.jpg", "Hello world!", new Font("华文新伟", 40, FontStyle.Bold), Brushes.Azure, 500, 50, "D: /fj3.jpg");
//ui.PictureImageWatermark("D:/fengjing.jpg","D:/log.png",680, 380, "D:/fj4.jpg");

Original picture:

2.1 Original image cropping

PictureCut(Async)
Statement: void PictureCut(string SourceFile, int CutStartPointX, int CutStartPointY, int CutWidth, int CutHeight, string TargetFile)
Purpose: picture cropping
Parameters: (1) string SourceFile -- original image file path
     (2) int CutStartPointX --X coordinate of the starting cutting point
     (3) int CutStartPointY – Y coordinate of the starting cutting point
     (4) int CutWidth – cutting width
     (5) int CutHeight – cutting height
     (6) string TargetFile --newly generated target image file path
Returns: (none)

Cropped image:

2.2 Original image thumbnail

PictureThumbnail(Async)
Statement: void PictureThumbnail(string SourceFile, int FrameWidth, int FrameHeight, string TargetFile)
Purpose: Image thumbnail
Parameters: (1) string SourceFile -- original image file path
     (2) int FrameWidth – the width of the thumbnail box
     (3) int FrameHeight – the height of the thumbnail box
     (4) string TargetFile --newly generated target image file path
Returns: (none)

Cropped image:

2.3 Add text watermark to original image

PictureTextWatermark(Async)
Statement: void PictureTextWatermark(string SourceFile, string WaterText, System.Drawing.Font WaterTextFont, System.Drawing.Brush WaterTextBrush, int x, int y, string TargetFile)
Purpose: Add "text" watermark to pictures
Parameters: (1) string SourceFile -- original image file path
     (2) string WaterText -- watermark text
     (3) System.Drawing.Font WaterTextFont -- watermark text font
(4) System.Drawing.Brush WaterTextBrush --watermark text stroke
(5) int x – the starting X coordinate of the watermark image
(6) int y – the starting Y coordinate of the watermark image
     (7) string TargetFile --newly generated target image file path
Returns: (none)

Cropped image:

2.4 Add image watermark to original image

PictureImageWatermark(Async)
Statement: void PictureImageWatermark(string SourceFile, string WatermarkFile, int x, int y, string TargetFile)
Purpose: Add "image" watermark to pictures
Parameters: (1) string SourceFile -- original image file path
     (2) string WatermarkFile -- watermark image file path
     (3) int x – the starting X coordinate of the watermark image
(4) int y – the starting Y coordinate of the watermark image
     (5) string TargetFile --newly generated target image file path
Returns: (none)

Cropped image:

3. Customized image cropping, scaling, and watermarking

To achieve cropping, scaling and watermarking of images, the specific implementation methods are as follows:

  1. Image cropping

Images can be read using the Bitmap class of C# and cropped through the DrawImage method of the Graphics class. Here is a simple example code:

Bitmap srcImage = new Bitmap(@"C:\image.jpg");
Rectangle cropArea = new Rectangle(100, 100, 200, 200); //Area to be cropped
Bitmap targetImage = new Bitmap(cropArea.Width, cropArea.Height);

using(Graphics g = Graphics.FromImage(targetImage))
{<!-- -->
    g.DrawImage(srcImage, new Rectangle(0, 0, targetImage.Width, targetImage.Height),
                cropArea, GraphicsUnit.Pixel);
}

targetImage.Save(@"C:\cropImage.jpg");

In the above code, first load the image that needs to be cropped, then define an area to be cropped (cropArea), and implement cropping through the DrawImage method of the Graphics class. Finally, save the cropped image.

  1. Image zoom

You can also use the DrawImage method of the Graphics class to zoom in and out of images. Here is a simple scaling example code:

int newWidth = 800; // new width
int newHeight = 600; // new height

Bitmap srcImage = new Bitmap(@"C:\image.jpg");
Bitmap targetImage = new Bitmap(newWidth, newHeight);

using(Graphics g = Graphics.FromImage(targetImage))
{<!-- -->
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    g.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}

targetImage.Save(@"C:\scaleImage.jpg");

In the above code, first load the image that needs to be scaled, then define the new width and height (newWidth and newHeight), and implement scaling through the DrawImage method of the Graphics class. Pay attention to setting the interpolation mode (InterpolationMode), smoothing mode (SmoothingMode) and pixel offset mode (PixelOffsetMode) to ensure the quality of the scaled image. Finally, save the zoomed image.

  1. add watermark

The Graphics class of C# provides a variety of methods to implement watermarks on images. The following is a simple sample code:

Bitmap srcImage = new Bitmap(@"C:\image.jpg");
Bitmap waterMark = new Bitmap(@"C:\watermark.png"); // Watermark image
Bitmap targetImage = new Bitmap(srcImage.Width, srcImage.Height);

using(Graphics g = Graphics.FromImage(targetImage))
{<!-- -->
    g.DrawImage(srcImage, new Rectangle(0, 0, targetImage.Width, targetImage.Height),
                new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
    g.DrawImage(waterMark, new Rectangle(10, 10, 100, 100)); // Watermark position
}

targetImage.Save(@"C:\watermarkImage.jpg");

In the above code, the image to be added with a watermark and the watermark image are first loaded, and then the overlay of the images is implemented through the DrawImage method of the Graphics class. You can adjust the position and size of the watermark by setting the position of the watermark image. Finally, save the image after adding the watermark.

It should be noted that some details need to be considered when implementing specific functions. For example, when cropping and scaling, you need to determine whether the aspect ratio of the picture is appropriate to avoid image deformation; when adding a watermark, you also need to consider the transparency of the watermark and other issues to ensure The final effect is as expected.

Thank you: a letter to readers

Dear readers,

I put a lot of thought and time into this article and hope to provide you with valuable content. This article contains in-depth research and personal experience, and I believe this information will be very helpful to you.

If you find this article helpful, I sincerely ask you to consider supporting it with a $1 donation. This amount will not be a burden on your finances, but it will have a positive impact on my ability to continue creating quality content.

I wrote this article because I love sharing useful knowledge and insights. Your support will help me continue this mission and encourage me to spend more time and energy creating more valuable content.

If you would like to support my creation, please scan the QR code below, your support will be greatly appreciated. At the same time, if you have any feedback or suggestions, please share them with me.

Thank you again for reading and supporting!

Sincerest regards, “Yugong moves the code”