Feedback

Bilder verkleinern, beschneiden und beides gleichzeitig

Sprache: C#

Dieser Snippet ist eine Übersetzung des Snippets von Chris_Cluss nach C# (http://dotnet-snippets.de/dns/resizecrop-und-beides-gleichzeitig-von-images-SID1169.aspx) Die Klasse bietet 3 Methoden um Bilder zu beschneiden und zu verkleinern. Der zurückgegebene MemoryStream kann mit folgendem Code wieder in ein Image gewandelt werden: [code]Image image = Image.FromStream(memoryStream);[/code]
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageFunctions
{
    /// <summary>
    /// Crops the and resize image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="targetWidth">Width of the target.</param>
    /// <param name="targetHeight">Height of the target.</param>
    /// <param name="x1">The position x1.</param>
    /// <param name="y1">The position y1.</param>
    /// <param name="x2">The position x2.</param>
    /// <param name="y2">The position y2.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the cropped and resized image.</returns>
    public MemoryStream CropAndResizeImage(Image img, int targetWidth, int targetHeight, int x1, int y1, int x2, int y2, ImageFormat imageFormat)
    {
        var bmp = new Bitmap(targetWidth, targetHeight);
        Graphics g = Graphics.FromImage(bmp);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        int width = x2 - x1;
        int height = y2 - y1;

        g.DrawImage(img, new Rectangle(0, 0, targetWidth, targetHeight), x1, y1, width, height, GraphicsUnit.Pixel);

        var memStream = new MemoryStream();
        bmp.Save(memStream, imageFormat);
        return memStream;
    }

    /// <summary>
    /// Resizes the image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="targetWidth">Width of the target.</param>
    /// <param name="targetHeight">Height of the target.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the resized image.</returns>
    public MemoryStream ResizeImage(Image img, int targetWidth, int targetHeight, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, targetWidth, targetHeight, 0, 0, img.Width, img.Height, imageFormat);
    }

    /// <summary>
    /// Crops the image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="x1">The position x1.</param>
    /// <param name="y1">The position y1.</param>
    /// <param name="x2">The position x2.</param>
    /// <param name="y2">The position y2.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the cropped image.</returns>
    public MemoryStream CropImage(Image img, int x1, int y1, int x2, int y2, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, x2 - x1, y2 - y1, x1, y1, x2, y2, imageFormat);
    }
}
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageFunctions
{
    /// <summary>
    /// Crops the and resize image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="targetWidth">Width of the target.</param>
    /// <param name="targetHeight">Height of the target.</param>
    /// <param name="x1">The position x1.</param>
    /// <param name="y1">The position y1.</param>
    /// <param name="x2">The position x2.</param>
    /// <param name="y2">The position y2.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the cropped and resized image.</returns>
    public MemoryStream CropAndResizeImage(Image img, int targetWidth, int targetHeight, int x1, int y1, int x2, int y2, ImageFormat imageFormat)
    {
        var bmp = new Bitmap(targetWidth, targetHeight);
        Graphics g = Graphics.FromImage(bmp);

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        int width = x2 - x1;
        int height = y2 - y1;

        g.DrawImage(img, new Rectangle(0, 0, targetWidth, targetHeight), x1, y1, width, height, GraphicsUnit.Pixel);

        var memStream = new MemoryStream();
        bmp.Save(memStream, imageFormat);
        return memStream;
    }

    /// <summary>
    /// Resizes the image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="targetWidth">Width of the target.</param>
    /// <param name="targetHeight">Height of the target.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the resized image.</returns>
    public MemoryStream ResizeImage(Image img, int targetWidth, int targetHeight, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, targetWidth, targetHeight, 0, 0, img.Width, img.Height, imageFormat);
    }

    /// <summary>
    /// Crops the image.
    /// </summary>
    /// <param name="img">The image</param>
    /// <param name="x1">The position x1.</param>
    /// <param name="y1">The position y1.</param>
    /// <param name="x2">The position x2.</param>
    /// <param name="y2">The position y2.</param>
    /// <param name="imageFormat">The image format.</param>
    /// <returns>MemoryStream of the cropped image.</returns>
    public MemoryStream CropImage(Image img, int x1, int y1, int x2, int y2, System.Drawing.Imaging.ImageFormat imageFormat)
    {
        return CropAndResizeImage(img, x2 - x1, y2 - y1, x1, y1, x2, y2, imageFormat);
    }
}

5 Kommentare

  1. Hi Jan,
    ich komm mit dem Snippet ned klar…
    Was muss ich für ImageFormat imageFormat reinstecken?
    imageFormat erkennt VS nicht 🙁

    hab das Zeug wie folgt implementiert:
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;

    namespace Core
    {
    public class ImageFunctions
    { … }
    }

    g.DrawImage(img, new Rectangle(0, 0, targetWidth, targetHeight), x1, y1, width, height, ImageFormat.Png);

  2. Hallo,

    Du kannst dort z.B. System.Drawing.Imaging.ImageFormat.Jpeg angeben.

    Hier ein Beispiel wie der ausführende Code der die Klasse benutzt dann aussehen könnte:

    ImageFunctions imageFunctions = new ImageFunctions();

    System.Drawing.Image meinBild = new System.Drawing.Bitmap(Server.MapPath(„meinBild.jpg“));

    MemoryStream myStream = imageFunctions.ResizeImage(meinBild, 200, 200, System.Drawing.Imaging.ImageFormat.Jpeg);

    System.Drawing.Image ResizedImage = new System.Drawing.Bitmap(myStream);

    ResizedImage.Save(Server.MapPath(„meinNeuesBild.jpg“), System.Drawing.Imaging.ImageFormat.Jpeg);

    Gruß,
    Markus