简体   繁体   中英

Convert a 2D array of ints to a greyscale image

I have a 2D array of ints ranging from 0 to 255, each representing a shade of grey. I need to turn it into a greyscale image. The width and height of the image are the number of columns and rows of the array, respectivly.

I am using Microsoft Visual C# 2010 Express with the latest (I think) .NET framework as of Feb, 2013.

Many other people have had this problem but none of the solutions posted have worked for me; they all seem to call methods that don't exist in my code. I think I might missing a using statement or something.

By the way I am very new to programming, so please explain everything as mush as possible.

Thanks in advance.

EDIT: Okay, this is what I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int width;
            int height;
            int[,] pixels;
            Random randomizer = new Random();

            Start:
            Console.WriteLine("Width of image?");
            string inputWidth = Console.ReadLine();
            Console.WriteLine("Height of image?");
            string inputHeight = Console.ReadLine();

            try
            {
                width = Convert.ToInt32(inputWidth);
                height = Convert.ToInt32(inputHeight);
            }
            catch (FormatException e)
            {
                Console.WriteLine("Not a number. Try again.");
                goto Start;
            }
            catch (OverflowException e)
            {
                Console.WriteLine("Number is too big. Try again.");
                goto Start;
            }

            pixels = new int[width, height];

            for (int i = 0; i < width; ++i)
                for (int j = 0; j < height; ++j)
                pixels[i, j] = randomizer.Next(256);



            Console.ReadKey();
        }
    }
}

So here's some pseudo code of what I'm trying to do:

Initialize some variables

Prompt user for preferred width and height of the resulting image.

Convert input into Int.

Set up the array to be the right size.

Temporary loop to fill the array with random values. (this will be replaced with a series of equations when I can figure out how to write to a PNG or BMP.

//This is where I would then convert the array into an image file.

Wait for further input.

The other solutions that seemed to have helped other people use a class or object called bitmap, but I don't seem to have that class, nor do I know what library it is in.

Create it the same way you would an image from RGB bytes, the only difference for Grey scale is that RGB will be the same value to get greys:

int width = 255; // read from file
int height = 255; // read from file
var bitmap = new Bitmap(width, height, PixelFormat.Canonical);

for (int y = 0; y < height; y++)
   for (int x = 0; x < width; x++)
   {
      int red = 2DGreyScaleArray[x][y]; // read from array
      int green = 2DGreyScaleArray[x][y]; // read from array
      int blue = 2DGreyScaleArray[x][y]; // read from array
      bitmap.SetPixel(x, y, Color.FromArgb(0, red, green, blue));
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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