views:

177

answers:

3

Hi there,

I have an image that is 240x320 (iphone camera image in portrait), and I need to programmatically (in C#) add white "bars" to the sides increasing the full image size to 320x320. I don't want to scale the image because that would mess up the aspect ratio.

I have found a lot of info about how to remove white bars with c#, but nothing about how to add them. I am at a loss. Does anyone have any input that might lead me the correct direction?

Thanks a bunch, Brett

+3  A: 

Create a new empty white bitmap of the desired size and blit the smaller image onto it.

Matti Virkkunen
A: 

Basically create a new bitmap with the required dimension, clear it with the color you want and then draw the smaller bitmap so that it is centered vertically.

Lucero
+4  A: 
Image src = Image.FromFile("picture.jpg");
Bitmap bmp = new Bitmap(320, 320);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White)
g.DrawImageUnscaled(src, 60, 0, 240, 320);
bmp.Save("file.jpg", ImageFormat.Jpeg);

Remember to dispose the object after use ;)

munissor
Awesome! Thanks a bunch!
Brett
The code for @Matti's answer. +1 for Matti!!
kenny
This is a good candidate for the using statement to handle the disposal.E.g. using (Bitmap bmp = new Bitmap(320, 320)){ //...}
Daniel Ballinger
@Daniel I wrote only the relevant code, but your comment is good. Notice that also the Graphics and the source image are good using candidates
munissor