Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:36:24+00:00 2026-05-28T20:36:24+00:00

I have an Image file that I would like to crop and resize at

  • 0

I have an Image file that I would like to crop and resize at the same time using the System.Drawing class

I am trying to build upon the ideas found in this article :http://www.schnieds.com/2011/07/image-upload-crop-and-resize-with.html

I am able to Crop and Resize seperately but when I try to combine the process, I am getting some strange output.

Here is what I have been trying

using (System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(w, h))
{
    _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    using (Graphics _graphic = Graphics.FromImage(_bitmap))
    {
        _graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        _graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        _graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        _graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

        //Code used to crop
        _graphic.DrawImage(img, 0, 0, w, h);
        _graphic.DrawImage(img, new Rectangle(0, 0, w, h), x, y, w, h, GraphicsUnit.Pixel);

        //Code I used to resize
        _graphic.DrawImage(img, 0, 0, img.Width, img.Height);
        _graphic.DrawImage(img, new Rectangle(0, 0, W_FixedSize, H_FixedSize), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);



       //continued...
    }
}

In the above code…there are two sections commented…one to crop and one one to resize.

For cropping, I pass in the proper coords and width/height part of the image to crop(x, y, w, h).

I would like to crop based on my parameters and draw the image based on the W_FixedSize and H_Fixed size params.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:36:25+00:00Added an answer on May 28, 2026 at 8:36 pm

    One thing all of the answers missed is that the resulting image will have a 50% transparent 1 pixel border around the image, due to a bug in GDI.

    To properly crop and resize, you need to apply the following settings to the graphics object:

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
    

    Then you need to make an ImageAttributes instance to fix the border bug:

    ImageAttributes ia = new ImageAttributes();
    ia.SetWrapMode(WrapMode.TileFlipXY);
    

    Then, when calling DrawImage, pass ia as the last parameter.

    If you’re dealing with any PNG, TIFF, or ICO images and converting them to a format that doesn’t support transparency, you also need to call g.Clear(bgcolor) prior to calling DrawImage.

    If you’re encoding to jpeg format, make sure to set the Quality parameter and dispose of the EncoderParameters object afterwards.

    The Bitmap instance that you are reading from will lock the underlying file until after it is disposed. If you use the FromStream method, you must keep the stream open until after the Bitmap instance is disposed. A good way to do this is clone the stream into a MemoryStream instance and assign it to the Bitmap.Tag property.

    I have a more complete list of GDI+ cropping & resizing bugs to avoid on my blog.

    I usually try to push people to use my imageresizing.net library, as it’s designed to operate safely on a website with optimum performance. 1 line of code, and very little room for user error.

    I downloaded Schnieds’ example project, and I have to say it’s an (unnecessarily) complicated way of doing things. Non-destructive editing is actually much easier, as shown on this article. It’s easy to combine with Uploadify, although I don’t cover that on the blog.

    Also, re-encoding the image during upload is very destructive, both for jpeg and png files. Validation is good, but just dispose the instance after validation, don’t re-encode it. Schnieds’ example also leaks memory through the undisposed Bitmap instance – running it on a high-volume server would crash it quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large Png image file that I would like to display and
I have an .aspx file that outputs an image using the following methods: Server.MapPath(somefile.png)
I have an image that is served as static file using Nginx, but I
I have two png image files that I would like my android app to
Using the HTML5 <canvas> element, I would like to load an image file (PNG,
I have a project that creates a disk image and would like to use
I have an Image object that I would like to convert to an Icon
I have the following simple kml which references an image file that I would
I have a Java Applet that is generating an image. Ultimately, I would like
I have an ISO image that I would like to distribute. However, to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.