Kevin Parker
Kevin Parker

Reputation: 17206

Don't scale image when creating new bitmap from existing image

Does anyone know how to create a new bitmap from an existing image with a taller height, but don't scale the image and just have transparent, black or white below the original image in the new bitmap?

I basically have one picture that is taller than the second and I need the second one to be as tall as the first, without stretching it.

            img2 = new Bitmap(lImages[2],new Size(pictureBox.Image.Width,pictureBox.Image.Height));
            img2 = ((Bitmap)img2).Clone(new Rectangle(0, 0, pictureBox.Image.Width, pictureBox.Image.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);

C# .NET 4.0.

Upvotes: 1

Views: 441

Answers (2)

Ken Wayne VanderLinde
Ken Wayne VanderLinde

Reputation: 19349

By using a Graphics object, you can achieve this easily:

Bitmap temp = new Bitmap(new Size(pictureBox.Image.Width,pictureBox.Image.Height));
using(Graphics g = Graphics.FromImage(temp))
{
    g.DrawImage(img2, 0, 0);
}
img2 = temp;

Now img2 references a new Bitmap object of the required size which has the original (unstretched) image painted on it.

Note: To control the color of the extra space, add a call to g.FillRect before drawing the image.

Upvotes: 3

Tigran
Tigran

Reputation: 62246

Create your "standart" size bitmap and fill it with, let's say, white color and call Bitmap.MakeTransparent(Color.White) and draw your final image over it.

Upvotes: 0

Related Questions