裁剪 Graphics 对象?
我需要对图像做两件事:调整其大小,然后裁剪它。
我像这样调整大小:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
新位图的重载之一是宽度、高度、图形对象。您应该能够只传递图形对象,然后从中创建位图。像这样的东西
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