How to crop an image using C#?

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

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

相关标签:
14条回答
  • 2020-11-22 05:49

    It's quite easy:

    • Create a new Bitmap object with the cropped size.
    • Use Graphics.FromImage to create a Graphics object for the new bitmap.
    • Use the DrawImage method to draw the image onto the bitmap with a negative X and Y coordinate.
    0 讨论(0)
  • 2020-11-22 05:49

    Cropping an image is very easy in C#. However, doing the stuff how are you going to manage the cropping of your image will be a little harder.

    Sample below is the way how to crop an image in C#.

    var filename = @"c:\personal\images\horizon.png";
    var img = Image.FromFile(filename);
    var rect = new Rectangle(new Point(0, 0), img.Size);
    var cloned = new Bitmap(img).Clone(rect, img.PixelFormat);
    var bitmap = new Bitmap(cloned, new Size(50, 50));
    cloned.Dispose();
    
    0 讨论(0)
  • 2020-11-22 05:51

    There is a C# wrapper for that which is open source, hosted on Codeplex called Web Image Cropping

    Register the control

    <%@ Register Assembly="CS.Web.UI.CropImage" Namespace="CS.Web.UI" TagPrefix="cs" %>

    Resizing

    <asp:Image ID="Image1" runat="server" ImageUrl="images/328.jpg" />
    <cs:CropImage ID="wci1" runat="server" Image="Image1" 
         X="10" Y="10" X2="50" Y2="50" />
    

    Cropping in code behind - Call Crop method when button clicked for example;

    wci1.Crop(Server.MapPath("images/sample1.jpg"));

    0 讨论(0)
  • 2020-11-22 05:53

    use bmp.SetResolution(image.HorizontalResolution, image .VerticalResolution);

    this may be necessary to do even if you implement best answer here especially if your image is real great and resolutions are not exactly 96.0

    My test example:

        static Bitmap LoadImage()
        {
            return (Bitmap)Bitmap.FromFile( @"e:\Tests\d_bigImage.bmp" ); // here is large image 9222x9222 pixels and 95.96 dpi resolutions
        }
    
        static void TestBigImagePartDrawing()
        {
            using( var absentRectangleImage = LoadImage() )
            {
                using( var currentTile = new Bitmap( 256, 256 ) )
                {
                    currentTile.SetResolution(absentRectangleImage.HorizontalResolution, absentRectangleImage.VerticalResolution);
    
                    using( var currentTileGraphics = Graphics.FromImage( currentTile ) )
                    {
                        currentTileGraphics.Clear( Color.Black );
                        var absentRectangleArea = new Rectangle( 3, 8963, 256, 256 );
                        currentTileGraphics.DrawImage( absentRectangleImage, 0, 0, absentRectangleArea, GraphicsUnit.Pixel );
                    }
    
                    currentTile.Save(@"e:\Tests\Tile.bmp");
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 05:55

    Assuming you mean that you want to take an image file (JPEG, BMP, TIFF, etc) and crop it then save it out as a smaller image file, I suggest using a third party tool that has a .NET API. Here are a few of the popular ones that I like:

    LeadTools
    Accusoft Pegasus Snowbound Imaging SDK

    0 讨论(0)
  • 2020-11-22 05:56

    Check out this link: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

    private static Image cropImage(Image img, Rectangle cropArea)
    {
       Bitmap bmpImage = new Bitmap(img);
       return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
    }
    
    0 讨论(0)
提交回复
热议问题