我一直试图通过给出图像的坐标来获取图像的部分。这是从原始图像中提取图像的代码。
private byte[] ExtractImageSection(Bitmap originalMap, ImagePart imgPart)
{
Bitmap imageSection = new Bitmap(imgPart.Width, imgPart.Height);
int top = Math.Min(imgPart.Y1, imgPart.Y2);
int bottom = Math.Max(imgPart.Y1, imgPart.Y2);
int left = Math.Min(imgPart.X1, imgPart.X2);
int right = Math.Max(imgPart.X1, imgPart.X2);
Rectangle cloneRect = Rectangle.FromLTRB(left, top, right, bottom);
Graphics g = Graphics.FromImage(imageSection);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (imageSection)
//g.DrawImage(originalMap, 0, 0, cloneRect, GraphicsUnit.Pixel);
g.DrawImage(originalMap, cloneRect, 0, 0, imgPart.Width, imgPart.Height, GraphicsUnit.Pixel);
/* Code Commented - Clone Bitmap method
int top = Math.Min(imgPart.Y1, imgPart.Y2);
int bottom = Math.Max(imgPart.Y1, imgPart.Y2);
int left = Math.Min(imgPart.X1, imgPart.X2);
int right = Math.Max(imgPart.X1, imgPart.X2);
Rectangle cloneRect = Rectangle.FromLTRB(left, top, right, bottom);
System.Drawing.Imaging.PixelFormat pixelFormat = originalMap.PixelFormat;
Bitmap imageSection = (Bitmap)originalMap.Clone(cloneRect, pixelFormat);*/
imageSection.Save(pageImageLocation + imgPart.ImagePartName, ImageFormat.Png);
byte[] imageSectionByte = ConvertBitmapIntoByte(imageSection);
return imageSectionByte;
}
注释代码代表了迄今为止我为实现目标所使用的方法。我使用以下堆栈溢出链接尝试解决问题,但我仍然没有工作。这是原图(originalMap
)
上面的函数试图获取突出显示的部分。该函数X1, Y1, X2, Y2, Width, Heigth
从前端获取。为了获得所需的值,我正在使用
imgAreaSelect
到目前为止我为解决这个问题所做的事情
- 我还调试了代码以验证提取部分的坐标、宽度和高度。(他们是正确的)
- 尝试过
DrawImage
fromGraphics
类的变体。 - 尝试根据部分坐标克隆原始图像的
Clone
方法,您可以看到注释代码。Bitmap
这是提取方法后的图像。
谁能帮我弄清楚这个问题的解决方法?