图像裁剪不适用于tiff文件 [英] Image cropping not work with tiff-files
本文介绍了图像裁剪不适用于tiff文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有想要裁剪为多边形的图像。我的电脑上的所有图像都正常,但我有tiff文件。如果我尝试裁剪这个 - 结果我看到拉伸的文件。
I have image that i wanna crop as polygon. With all images on my pc all work correct, but i have tiff files. If i try crop this - in result i see stretched piece of file.
var img = (Bitmap)(GetPageFromTiff(openFileDialog.FileName));
img = CropImage(img);
pictureBox.BackgroundImage = img;
pictureBox.BackgroundImageLayout = ImageLayout.Zoom;
pictureBox.Image = img;
private Image GetPage(string file) {
Bitmap bitmap = (Bitmap) Image.FromFile(file);
bitmap.SelectActiveFrame(FrameDimension.Page, 0);
MemoryStream byteStream = new MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);
return Image.FromStream(byteStream);
}
public Bitmap CropImage(Image img) {
GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
var points = new Point[] {
new Point(85, 1111), new Point(934, 1111), new Point(934, 952), new Point(1642, 952),
new Point(1642, 2000), new Point(85, 2000), new Point(85, 1111)
};
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(points.ToArray());
Bitmap bmp1 = new Bitmap(2220, 2220);
using(Graphics G = Graphics.FromImage(bmp1)) {
G.Clip = new System.Drawing.Region(gp);
G.DrawImage(img, 0, 0);
G.Dispose();
}
pictureBox.Image = bmp1;
return bmp1;
}
https://snag.gy/UI3ZiA.jpg [ ^ ]
我尝试过:
我尝试将tiff保存为png,bmp等。全都一样。 Tiff文件下载 http://www.filedropper.com/165173z [ ^ ]
推荐答案
如果我从Stack Overflow复制的代码: http:// stackoverflow .com / questions / 30954503 /如何裁剪多边形区域 - 从一个图像在winform-picturebox [ ^ ] br />
稍微调整以适合测试图像:
If I take the code you copied from Stack Overflow: http://stackoverflow.com/questions/30954503/how-to-crop-a-polygonal-area-from-an-image-in-a-winform-picturebox[^]
And tweak it slightly to fit a test image:
Point[] points = new Point[] { new Point(60, 10), new Point(100, 60), new Point(60, 110), new Point(10, 60), new Point(60, 10) };
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(points.ToArray());
Bitmap bmp1 = new Bitmap(120, 120);
using (Bitmap bmp0 = (Bitmap)Bitmap.FromFile(imagePath))
{
using (Graphics G = Graphics.FromImage(bmp1))
{
G.Clip = new System.Drawing.Region(gp);
G.DrawImage(bmp0, 0, 0);
bmp1.Save(imageOutPath, ImageFormat.Jpeg);
}
}
它工作正常 - 我在黑色图像中得到一个图像钻石。
查看您的区域,它不适合您的输出图像...这可以解释问题...
It works fine - I get a diamond of image inside a black image.
Looking at your region, it doesn't fit inside your output image...which may explain the problem...
这篇关于图像裁剪不适用于tiff文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文