0

0 から 255 の範囲の int の 2D 配列があり、それぞれが灰色の色合いを表しています。それをグレースケール画像に変換する必要があります。画像の幅と高さは、それぞれ配列の列と行の数です。

2013 年 2 月現在、Microsoft Visual C# 2010 Express を最新の (と思う) .NET フレームワークで使用しています。

他の多くの人がこの問題を抱えていますが、投稿された解決策はどれもうまくいきませんでした。それらはすべて、私のコードに存在しないメソッドを呼び出しているようです。using ステートメントか何かが欠落している可能性があると思います。

ところで、私はプログラミングに非常に慣れていないので、できるだけすべてを説明してください。

前もって感謝します。

編集:さて、これは私が持っているものです:

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();
        }
    }
}

だからここに私がやろうとしていることのいくつかの擬似コードがあります:

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.

他の人がビットマップと呼ばれるクラスまたはオブジェクトを使用するのに役立つと思われる他の解決策ですが、私はそのクラスを持っていないようで、それがどのライブラリにあるのかわかりません.

4

1 に答える 1

0

RGB バイトから画像を作成するのと同じ方法で作成します。グレー スケールの唯一の違いは、RGB がグレーを取得するために同じ値になることです。

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));
   }
于 2013-02-19T05:25:45.733 に答える