C#图像处理(2):给图片加白边

C#图片处理给图片添加白边:

        /// <summary>
        /// 在图片上方加入白边
        /// </summary>
        /// <param name="Img">图片</param>
        /// <param name="Margin">白边的高度,单位是像素</param>
        /// <returns>Bitmap</returns>
        public Bitmap WhiteUp(Image Img, int Margin)
        {
            //获取图片宽高
            int Width = Img.Width;
            int Height = Img.Height;
            //获取图片水平和垂直的分辨率
            float dpiX = Img.HorizontalResolution;
            float dpiY = Img.VerticalResolution;
            //创建一个位图文件
            Bitmap BitmapResult = new Bitmap(Width, Height + Margin, PixelFormat.Format24bppRgb);
            //设置位图文件的水平和垂直分辨率  与Img一致
            BitmapResult.SetResolution(dpiX, dpiY);
            //在位图文件上填充一个矩形框
            Graphics Grp = Graphics.FromImage(BitmapResult);
            System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Width, Height + Margin);
            //定义一个白色的画刷
            SolidBrush mySolidBrush = new SolidBrush(System.Drawing.Color.White);
            //Grp.Clear(Color.White);
            //将矩形框填充为白色
            Grp.FillRectangle(mySolidBrush, Rec);
            //Grp.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //Grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //Grp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //向矩形框内填充Img
            Grp.DrawImage(Img, 0, Margin, Rec, GraphicsUnit.Pixel);
            //返回位图文件
            Grp.Dispose();
            GC.Collect();
            return BitmapResult;
        }

 

posted on 2014-08-18 15:09  吴培洪  阅读(3290)  评论(0编辑  收藏  举报

导航