【问题标题】:Taking pixels from one image and enlarging them on a new image to create a larger version of original从一张图像中获取像素并将它们放大到新图像上以创建更大版本的原始图像
【发布时间】:2014-01-28 00:37:09
【问题描述】:

我正在尝试制作一个图像放大器,它可以拍摄 500x500 像素的图像并输出完全相同且没有失真的 1000x1000 像素图像。我希望它像这样工作:对于原始图像中的每个像素,它应该将与该像素相同颜色的 4 个像素添加到新图像中,并对每个像素重复。因此,原来的 1 个像素变成了新的 4 个像素(正方形中有 4 个像素)。我试图做到这一点,但我无法让它正常工作。到目前为止我已经写了这个,但它只是在角落的 1000x1000 像素图像上输出相同尺寸的图像:

        Bitmap oldImg = new Bitmap(Image.FromFile(@"C:\ServerSystem\Testing\20140127T154500.png"));
        Bitmap newImg = new Bitmap(1000, 1000);

        System.Drawing.Imaging.BitmapData data = oldImg.LockBits(new Rectangle(0, 0, 500, 500), System.Drawing.Imaging.ImageLockMode.ReadOnly, oldImg.PixelFormat);
        oldImg.UnlockBits(data); byte[] rgba = new byte[data.Stride * 500];
        System.Runtime.InteropServices.Marshal.Copy(data.Scan0, rgba, 0, data.Stride * 500);

        using (Graphics g = Graphics.FromImage(newImg))
        {
            for (int x = 0; x < 500; x++)
            {
                for (int y = 0; y < 500; y++)
                {
                    newImg.SetPixel(x, y, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x + 1, y, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x, y + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x + 1, y + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                }
            }
            newImg.Save(@"C:\ServerSystem\Testing\20140127T154500HIRES.png");
        }

我怎样才能让它正常工作并让它从 1 中产生 4 像素?谢谢

【问题讨论】:

  • 你在新镜像中的索引都是错误的。 xy0 运行到 499,所以除了顶角之外你永远不会画任何东西。你需要的是newImg.SetPixel(x*2, y*2, ...)(x*2)+1,(y*2)+1等等。
  • 另外:exactly the same and with no distortion - 图像大小加倍不是定义上的失真吗?
  • 我的意思是没有失真,就像当您调整图像大小并尝试将像素拉伸到多个像素时一样,您会得到所有的边缘和抗锯齿。我正在尝试创建一个放大的完美副本。
  • @MattBurland 如果您将“0”计为有效索引,这是否意味着您仍在设置 500 像素?
  • @HenryHunt 如果不使用像素数据,为什么要将它复制到该字节数组? (GetPixelSetPixel 不需要它)

标签: c#


【解决方案1】:

由于您只想将其放大两倍,因此您几乎完成了。 您所要做的就是:

for (int x = 0; x < 500; x++)
{
  for (int y = 0; y < 500; y++)
  {
    newImg.SetPixel(x*2, y*2, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2 + 1, y*2, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2, y*2 + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2 + 1, y*2 + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
  }
}

【讨论】:

  • + 1 * 2 在最后一行??
猜你喜欢
  • 2011-07-03
  • 2013-09-25
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多