C# image whitespace

前端 未结 4 1785
面向向阳花
面向向阳花 2021-02-13 14:35

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 320x3

相关标签:
4条回答
  • 2021-02-13 15:09

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

    0 讨论(0)
  • 2021-02-13 15:11
    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 ;)

    0 讨论(0)
  • 2021-02-13 15:21

    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

    0 讨论(0)
  • 2021-02-13 15:28

    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.

    0 讨论(0)
提交回复
热议问题