22 Jun

C# Resize A Bitmap Example

This example shows how to resize a bitmap file in C#. To resize a bitmap image, we use Graphics class in System.Drawing namespace.

Usage:

            //Size of "testimage.bmp" file is 1024x1024.
            Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\testimage.bmp");
            Bitmap newImage = ResizeBitmap(bmp, 512, 512);

Resize Method Example:

        public Bitmap ResizeBitmap(Bitmap bmp, int width, int height)
        {
            Bitmap result = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(result))
            {
                g.DrawImage(bmp, 0, 0, width, height);
            }

            return result;
        }

2 thoughts on “C# Resize A Bitmap Example

Leave a Reply

Your email address will not be published. Required fields are marked *