DrawImage(.NET system.drawing.drawing2D)中的灰色边框 [英] Grey borders in DrawImage (.NET system.drawing.drawing2D)

查看:72
本文介绍了DrawImage(.NET system.drawing.drawing2D)中的灰色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#在位图上创建图像.它总是以某种方式在图像的所有侧面上添加灰色边框.解决方案中发布了许多问题,我几乎尝试了所有问题,但没有一个起作用.

I'm trying to create an image on a bitmap using C#. Somehow it always adds grey border on all sides of the image. Many questions have been posted with the solution and I have tried almost all of them but none of them worked.

我正在使用的代码:

Bitmap appearance = new Bitmap(490, 196, PixelFormat.Format32bppArgb);

using (Graphics graphics = Graphics.FromImage(appearance))
{
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.PixelOffsetMode = PixelOffsetMode.Half;
    graphics.CompositingMode = CompositingMode.SourceCopy;

    using (ImageAttributes wrapMode = new ImageAttributes())
    {
        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
        Rectangle destination = new Rectangle(5, 99, 240, 94);
        graphics.DrawImage(img, destination, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, wrapMode);
    }
}
   
appearance.Save("F:\\SignatureAppearance.jpeg");

有人可以帮助我吗?缩小时不会显示灰色边框,但是当人们开始放大时,边框开始出现.

Can anyone help me? The grey border does not show when zoomed out but as one starts zooming in, borders start appearing.

任何帮助将不胜感激

我尝试创建具有白色背景的图像,即使该图像具有右边界和底边界.

i have tried to create an image with white background even that has right and bottom borders .

Bitmap appearance = new Bitmap(490, 196, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(appearance)) { 
// graphics.Clear(Color.White); 
   Rectangle ImageSize = new Rectangle(0, 0, 490, 196); 
   graphics.FillRectangle(Brushes.White, ImageSize); } 
    appearance.Save("F:\\Signature_Appearance.png", ImageFormat.Png);

}

推荐答案

问题似乎是您没有指定保存格式,而是使用 PixelFormat.Format32bppArgb 是PNG.路径上的文件扩展名将被忽略.

The problem appears to be that you haven't specified a save format, and the default save format for an image with PixelFormat.Format32bppArgb is PNG. The file extension on your path is ignored.

您需要将最后一行更改为

You would need to change your last line to

appearance.Save(@"F:\SignatureAppearance.jpeg", ImageFormat.Jpeg);

实际获得JPEG输出.

to actually get a JPEG output.

此外,除非您的示例中省略了该代码,否则看来您可能尚未填充所创建的整个 Bitmap .为了确保白色背景,您需要用白色填充所有未定义的像素,因为在JPEG编码器中透明像素被映射为黑色.上面的代码段仅显示图像的 destination 子区域已定义.您可以通过插入

Additionally, it appears you may not have filled the entire Bitmap you created, unless that code is omitted from your example. To ensure a white background, you would need to fill any undefined pixels with white, because transparent pixels are mapped to black in the JPEG encoder. Your code snippet above only shows that the destination sub-area of the image is defined. You can fill the missing area by inserting

graphics.Clear(Color.White);

在绘制子矩形之前.

这篇关于DrawImage(.NET system.drawing.drawing2D)中的灰色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆