简体   繁体   English

如何在图形上绘制图像而在外边界处没有额外的像素?

[英]How to draw image on Graphics without extra pixels at outer borders?

This simple code gives me very bad result. 这个简单的代码给我非常糟糕的结果。

            Image global_src_img = Image.FromFile(Application.StartupPath + "\\src.png");
            Image src_img = ((Bitmap)global_src_img).Clone(new Rectangle(160, 29, 8, 14), PixelFormat.Format32bppArgb);
            src_img.Save(Application.StartupPath + "\\src_clonned.png", ImageFormat.Png);
            Bitmap trg_bmp = new Bitmap(100, 100);
            Graphics gfx = Graphics.FromImage(trg_bmp);
            gfx.FillRectangle(new Pen(Color.FromArgb(255, 0, 0, 0)).Brush, 0, 0, trg_bmp.Width, trg_bmp.Height);
            gfx.DrawImageUnscaled(src_img, (trg_bmp.Width / 2) - (src_img.Width / 2), (trg_bmp.Height / 2) - (src_img.Height / 2));
            gfx.Dispose();
            trg_bmp.Save(Application.StartupPath + "\\trg.png", ImageFormat.Png);

It clones part of the big image with letters and writes it to another image. 它用字母克隆大图像的一部分,并将其写入另一个图像。 Written image has extra pixels at outer borders that does not present in source image same as in cloned image. 书面图像在外边界具有额外的像素,该像素在源图像中与克隆图像中不同。 How to avoid it? 如何避免呢?

This is cloned image ( src_clonned.png ) that later will be drawn on graphics. 这是克隆的图像( src_clonned.png ),稍后将在图形上绘制。 在此处输入图片说明

This is saved resulting image ( trg.png ). 这将保存结果图像( trg.png )。 在此处输入图片说明

Draw directly on bitmap to avoid any pixels modifications with this: 直接在位图上绘制,以避免对此进行任何像素修改:

public static void DrawImgOnImg(Image src, Bitmap trg, int x, int y)
{
    for (int i = x; i < x + src.Width; i++)
        for (int j = y; j < y + src.Height; j++)
        {
            Color src_px = ((Bitmap)src).GetPixel(i - x, j - y);
            trg.SetPixel(i, j, src_px);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM