【问题标题】:Set the pixels of image to the values stored in an array of integers将图像的像素设置为整数数组中存储的值
【发布时间】:2013-05-18 08:56:33
【问题描述】:
namespace txtToImg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string fileContent = File.ReadAllText("D:\\pixels.txt");

            string[] integerStrings = fileContent.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            int[] integers = new int[integerStrings.Length];

            for (int n = 0; n < integerStrings.Length; n++)
            {
                integers[n] = int.Parse(integerStrings[n]);
            }

            Bitmap my = new Bitmap(512, 512);

            for (int i = 0; i < 512; i++)
                for (int j = 0; j < 512; j++)
                    my.SetPixel(i, j, Color.Blue);
            my.Save("D:\\my.jpg");

        }
    }
}

我想使用数组中的值,而不是像我所做的那样将所有像素设置为蓝色。

这就是我将像素保存到文本文件的方式!它们是从 0 到 255 的整数。现在我正在尝试处理灰度图像,所以我不需要单独的 R、G 和 B,这就是 (R+G+B)/3 的原因。

using (Bitmap bitmap = new Bitmap("D:\\6.jpg"))
    {
        int width = 512;
        int height = 512;
        TextWriter tw = new StreamWriter("D:\\pixels.txt");

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                Color color = bitmap.GetPixel(j, i);
                tw.Write((color.R + color.G + color.B) / 3 + " ");
            }
            //tw.Write(" ");
        }
        tw.Close();
    }

【问题讨论】:

  • 整数值中的颜色是如何编码的?
  • 我编辑了我的问题,你可以在那里看到答案!谢谢!

标签: c# image text pixels


【解决方案1】:

由于您已经拥有每种颜色 1 个字节的灰度值,因此对新图像使用 Format8bppIndexed 是有意义的:

// Create 8 bit per pixel bitmap
var bitmap = new Bitmap(512, 512, PixelFormat.Format8bppIndexed);

// Set grayscale color palette
var colorPalette = bitmap.Palette;
var colorEntries = colorPalette.Entries;

for ( int i = 0; i < 256; i++ )
{
    colorEntries[i] = Color.FromArgb(i, i, i);
}

// Apply changes to color pallete
bitmap.Palette = colorPalette;

// Retrieve bitmap data for efficient writes
var bitmapData = bitmap.LockBits(Rectangle.FromLTRB(0, 0, 512, 512), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

// Allocate array to store intermediate pixel data
byte[] colorData = new byte[bitmapData.Stride * bitmapData.Height]; // 1 byte per pixel since it is 8bppIndexed format

for (int i = 0; integers.Length; i++)
{
    int line = i / 512;
    int position = i % 512;

    colorData[line * bitmapData.Stride + position] = (byte)integers[i]; // color values from file
}

// Copy computed pixel data to BitmapData
Marshal.Copy(colorData, 0, bitmapData.Scan0, colorData.Length);

bitmap.UnlockBits(bitmapData);

bitmap.Save("D:\\test.bmp");

【讨论】:

  • 感谢您的帮助!但是我得到了一张与原始图像没有共同点的彩色图像:(
  • 我已经更新了代码。更改颜色条目后应明确设置 Palette 属性。
  • OpenMinded,非常感谢!现在肯定行得通!我会花时间去理解它,以便下次我可以编写自己的代码:)再次感谢!
  • 你能告诉我应该改变什么,以便该代码适用于 RGB 图像,但不适用于灰度图像?
  • 首先PixelFormat应该不同——Format24bppRgb。 'for' 循环会更复杂,因为每个像素有 3 个字节。 colorData 应如下所示:R, G, B, R, G, B,... bitmapData.Stride - 是以字节为单位的单行大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
相关资源
最近更新 更多