简体   繁体   中英

I want to crop/cut an image on C#

The image i want to crop image cropped

I want to crop a image that i have, the size is static and the point is always (0, 0).

I tried somethings I saw in StackOverflow, but they didn't work

private void Converter(Image image, int count)
    {
        string name = String.Format("imgTemp\\img{0}.bmp", count);
        Image img = image;
        Bitmap org = new Bitmap(img, 866, 520);
        Bitmap target = org.Clone(new Rectangle(0, 0, 866, 520), PixelFormat.Format1bppIndexed); // I convert the image to monochrome and that size
        target.SetResolution(1, 1);
        if (File.Exists(name))
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            File.Delete(name);
            target.Save(name, ImageFormat.Bmp);
            target.Dispose();
        }
        else
        {
            target.Save(name, ImageFormat.Bmp);
            target.Dispose();
        }
        lbFicheiros.Items.Add(name);
    }

Your code is almost correct. You were declaring the org variable with the targeted height/width that is why you could not crop it because it was already that size.

private void Converter(Image image, int count)
        {
            string name = String.Format("imgTemp\\img{0}.bmp", count);
            Image img = image;
            Bitmap org = new Bitmap(img); //Changed line
            Bitmap target = org.Clone(new Rectangle(0, 0, 866, 520), PixelFormat.Format1bppIndexed); // I convert the image to monochrome and that size
            target.SetResolution(1, 1);
            if (File.Exists(name))
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                File.Delete(name);
                target.Save(name, ImageFormat.Bmp);
                target.Dispose();
            }
            else
            {
                target.Save(name, ImageFormat.Bmp);
                target.Dispose();
            }
            lbFicheiros.Items.Add(name);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM