将图像的像素设置为存储在整数数组中的值 [英] Set the pixels of image to the values stored in an array of integers

查看:114
本文介绍了将图像的像素设置为存储在整数数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");

        }
    }
}

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

Instead of setting all the pixels to Blue as I've done, I want to use the values from the array.

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

This is how I save the pixels to a text file! They are integers from 0 to 255. Now I'm trying to deal with greyscale images so I don't need the R, G and B separately, that's why it's (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();
    }


推荐答案

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

Since you already have grayscale values with 1 byte per color it makes sense to use Format8bppIndexed for new image:

// 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");

这篇关于将图像的像素设置为存储在整数数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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