C# image whitespace

喜你入骨 提交于 2019-12-03 11:39:57
munissor
using (System.Drawing.Image src = System.Drawing.Image.FromFile("picture.jpg"))
{
       using (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 ;)

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

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.

Pinte Dani

Here's a great link where a more generalized approach is described for resizing images and adding white bars, either at the top or at the bottom (depending of image orientation)c# Image resizing to different size while preserving aspect ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!