How to crop an image using C#?

后端 未结 14 1104
忘掉有多难
忘掉有多难 2020-11-22 05:16

How can I write an application that will crop images in C#?

14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 05:59

    This is another way. In my case I have:

    • 2 numeric updown controls (called LeftMargin and TopMargin)
    • 1 Picture box (pictureBox1)
    • 1 button that I called generate
    • 1 image on C:\imagenes\myImage.gif

    Inside the button I have this code:

    Image myImage = Image.FromFile(@"C:\imagenes\myImage.gif");
    Bitmap croppedBitmap = new Bitmap(myImage);
    croppedBitmap = croppedBitmap.Clone(
                new Rectangle(
                    (int)LeftMargin.Value, (int)TopMargin.Value,
                    myImage.Width - (int)LeftMargin.Value,
                    myImage.Height - (int)TopMargin.Value),
                System.Drawing.Imaging.PixelFormat.DontCare);
    pictureBox1.Image = croppedBitmap;
    

    I tried it in Visual studio 2012 using C#. I found this solution from this page

提交回复
热议问题