裁剪 Graphics 对象?

发布于 2024-12-11 23:30:41 字数 2084 浏览 0 评论 0原文

我需要对图像做两件事:调整其大小,然后裁剪它。

我像这样调整大小:

nonResizedImage = new Bitmap(imagePath);
Bitmap scaled = new Bitmap(preCropWidth, preCropHeight);
using (Graphics scaledGraphics = Graphics.FromImage(scaled)) 
{ // scale image to the sizeo f the image the user cropped on
    scaledGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    scaledGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
    scaledGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    scaledGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    scaledGraphics.Clear(ColorTranslator.FromHtml("#FFFFFF"));
    scaledGraphics.DrawImage(nonResizedImage, 0, 0, preCropWidth, preCropHeight);
}

现在我需要裁剪图像。我找到了一个可以执行此操作的函数:

static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
    try
    {
        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {
            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {
                bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                {
                    Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, OriginalImage.RawFormat);
                    return ms.GetBuffer();
                }
            }
        }
    }

    catch (Exception Ex)
    {
        throw Ex;
    }
}

但这需要图像作为输入。因此,我可以将调整大小代码的输出保存到磁盘,然后再次读回以进行裁剪,但这似乎效率低下。不过,我对 C# 中的图像处理不太了解。

如何裁剪我拥有的 scaledGraphics,而不先将其保存到磁盘?

I need to do two things to an image: resize it, then crop it.

I'm resizing like this:

nonResizedImage = new Bitmap(imagePath);
Bitmap scaled = new Bitmap(preCropWidth, preCropHeight);
using (Graphics scaledGraphics = Graphics.FromImage(scaled)) 
{ // scale image to the sizeo f the image the user cropped on
    scaledGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    scaledGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
    scaledGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    scaledGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    scaledGraphics.Clear(ColorTranslator.FromHtml("#FFFFFF"));
    scaledGraphics.DrawImage(nonResizedImage, 0, 0, preCropWidth, preCropHeight);
}

Now I need to crop the image. I've found a function that does this:

static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
    try
    {
        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {
            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {
                bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                {
                    Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, OriginalImage.RawFormat);
                    return ms.GetBuffer();
                }
            }
        }
    }

    catch (Exception Ex)
    {
        throw Ex;
    }
}

But this requires a image as input. So, I could save the output of my resize code to the disk, then read it back in again to do the crop, but this seems needlessly inefficient. I don't really know much about image manipulation in c# though.

How do I crop the scaledGraphics I have, without first saving it to the disk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-12-18 23:30:41

新位图的重载之一是宽度、高度、图形对象。您应该能够只传递图形对象,然后从中创建位图。像这样的东西

  static byte[] Crop(Graphics g, int Width, int Height, int X, int Y)
  {
      try
       {
          using (Bitmap bmp = new Bitmap(Width, Height, g))
          { 
           ...
          }
        }
        ......
    }

One of the overloads for new Bitmap is Width, Height, Graphics Object. You should be able to just pass the graphics object in and then create the bitmap from that. Something like this

  static byte[] Crop(Graphics g, int Width, int Height, int X, int Y)
  {
      try
       {
          using (Bitmap bmp = new Bitmap(Width, Height, g))
          { 
           ...
          }
        }
        ......
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文