Home > Mobile >  I want to crop/cut an image on C#
I want to crop/cut an image on C#

Time:12-29

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);
    }

CodePudding user response:

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);
        }
  • Related