Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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[^]

What I have tried:

I try save tiff as png, bmp and other. All the same. Tiff file for downloadhttp://www.filedropper.com/165173z[^]
Posted
Updated 7-Oct-16 0:34am
v6
Comments
Bernhard Hiller 7-Oct-16 3:44am    
How do you load those tiff files? Show us the relevant code.

1 solution

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:
C#
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...
 
Share this answer
 
Comments
Member 11266633 7-Oct-16 3:10am    
Your code and my code work correctly but its not work with this tiff-file
http://www.filedropper.com/165173z
OriginalGriff 7-Oct-16 4:06am    
If you think I am going to an unknown site to download an unknown file from a total stranger, you are wrong!
Start by checking if the file is valid: read it into an Image, and display it in a PictureBox. Does it look right? What size is it? If you save that as a JPG what happens? Does it work if you load the JPG version? And so forth...
Member 11266633 7-Oct-16 4:11am    
its tiff file - whats the troubles? give me file sharing service - i upload it.
Member 11266633 7-Oct-16 4:18am    
http://stackoverflow.com/questions/39904362/issue-with-picturebox-control
Member 11266633 7-Oct-16 4:18am    
It work with picturebox, look right but after crop operation - not work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900