Location>code7788 >text

NET 8 High Performance Cross-Platform Image Processing Library ImageSharp

Popularity:453 ℃/2024-11-06 13:37:33

preamble

Traditional libraries are feature-rich, but have platform limitations and are not suitable for cross-platform development. With the release of .NET 8, ImageSharp becomes a better choice.

ImageSharp is a fully open source, high-performance and cross-platform image processing library designed for . It provides rich image processing features and excellent performance for desktop applications, web applications and cloud services.

This article will introduce the main features and basic usage of ImageSharp to help you quickly get started with this powerful tool.

Projects

A fully managed, high-performance, cross-platform graphics library that supports installation and use in any .NET 6+ environment. It is suitable for a wide range of scenarios, including device-side, cloud, embedded and IoT.

ImageSharp is feature-rich and easy to use, and common operations can be done with just a few lines of code. It supports a wide range of image formats such as JPEG, BMP, GIF, PNG, TIFF, WebP and QOI. It also supports more than 25 pixel formats and provides metadata editing (e.g. IPTC, EXIF, XMP) and color space conversion (e.g. RGB, CMYK, grayscale, CIELab).

In addition, ImageSharp offers more than 40 common image processing operations with excellent performance and memory friendliness, capable of handling images of any size, including large multi-million pixel images.

Project use

1、Installation of dependent libraries

First you need to add it to your project. Installing it through the NuGet package manager is the easiest way, as shown below:

Install-Package 

Or VS direct search and install

2、Load the picture

Loading images with ImageSharp is very simple.

/// <summary>
/// Load Image
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadImage_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog
    {
        Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif"
    };

    if (() == true)
    {
        using (FileStream fs = new FileStream(, ))
        {
            _image = <Rgba32>(fs);
             = ConvertToBitmapSource(_image);
        }
    }
}

3、Crop the picture

Resizing images with ImageSharp

/// <summary>
/// Cropping pictures
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CropImage_Click(object sender, RoutedEventArgs e)
{
    if (_image != null)
    {
        _image.Mutate(x => (new (100, 100, 200, 200)));
         = ConvertToBitmapSource(_image);
    }
}

4、Rotate the picture

ImageSharp also makes it easy to rotate and flip images.

/// <summary>
/// Select Image
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RotateImage_Click(object sender, RoutedEventArgs e)
{
    if (_image != null)
    {
        _image.Mutate(x => (90));
         = ConvertToBitmapSource(_image);
    }
}

5. Running effect

There are more effects and methods that you can try for yourself.

Common methods

  • Load and save images
  • (): Loads the image file.
  • (): Saves the image to a file.
  • Resizing images
  • Resize(): Changes the size of the image.
  • Cropping images
  • Crop(): Crops the image to the specified rectangular area.
  • Rotate and flip images
  • Rotate(): Rotate the image by a certain angle.
  • Flip(): Flips the image horizontally or vertically.
  • Applying filters and effects
  • Grayscale(): Converts the image to grayscale.
  • GaussianBlur(): Apply Gaussian Blur effect.
  • Sharpen(): Sharpens the image.
  • Adjusting Brightness, Contrast and Saturation
  • Brightness(): Adjusts the brightness of the image.
  • Contrast(): Adjusts the contrast of the image.
  • Saturate(): Adjusts the saturation of the image.
  • Drawing images and text
  • DrawImage(): draws another image on top of the image.
  • DrawText(): draws text on the image.
  • pixel operation
  • ProcessPixelRows(): provides low-level access to image pixel rows.
  • Image format conversion
  • (): Detects the format of the image.
  • () and (): Load and save images in specific formats.
  • Image Metadata Processing
  • Metadata: Access and modify the metadata of the image.

Common Filters

  • Grayscale: Converts the image to grayscale, removing the color information and keeping only the brightness information.
  • BlackWhite (black and white filter): Converts the image to strictly black and white.
  • Sepia (sepia filter): Applies a sepia tone to the image to mimic the style of old photographs.
  • Invert: Inverts all colors in the image.
  • GaussianBlur: Applies a Gaussian blur to the image, which can be used to create a softening effect or to reduce image noise.
  • Sharpen: Enhances the contrast at the edges of the image to make it look sharper.
  • Vignette: Adds a dark corner effect to the edges of an image, often used to draw the viewer's attention to the center of the image.
  • Brightness: Adjusts the brightness of the image.
  • Contrast: Adjusts the contrast of the image.
  • Saturate: Adjusts the saturation of the image.
  • Hue (Hue Filter): Adjusts the hue of the image.
  • Pixelate (Pixelation Filter): Splits an image into large blocks of pixels to create a pixel art effect.
  • OilPaint: Imitates the effect of oil paint strokes.
  • Glow: Adds a soft glow around the image.
  • BokehBlur (Background Bokeh Filter): Creates an effect that mimics background bokeh (bokeh) in photography.
  • DetectEdges: Detects and highlights edges in the image.
  • Polaroid (Tone Separation Filter): Reduces the amount of color in an image to create a vintage poster-like effect.

Project Address

GitHub:/SixLabors/ImageSharp

summarize

ImageSharp is a powerful .NET image processing library that provides a wide range of image manipulation options, including loading, saving, resizing, rotating, flipping, and applying various filters and effects.

The examples in this article show only a part of the ImageSharp functionality, by using the ImageSharp library you can easily implement complex image processing tasks in the project.

I hope this article can provide you with useful help in image processing development. We look forward to your exchanges in the comments section to share your valuable experience and suggestions.

ultimate

If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.

You can also join WeChat[DotNet Technician] community to share ideas and grow with other tech-loving peers!Excellence is a habit, so feel free to leave a comment and learn!