我怎样才能读出正常的地图效果?

时间:2017-08-02 15:34:05

标签: c#

我编写了一个代码,用于读取图像并计算RGB值中每个像素的法线向量。问题是,即使我选择256x256图像c#也需要很长时间才能运行,有时会关闭。有人能告诉我另一种方法或不同的解决方案吗?

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Aufgabe17NormalMapping
{
public partial class Form1 : Form
{
    public delegate void TextManagerDelegate(string text);
    TextManagerDelegate TextHandler;

    Bitmap img;

    public Form1()
    {
        InitializeComponent();
        TextHandler = new TextManagerDelegate(TextManager);

    }


    public void TextManager(string txt)
    {
        textBox1.AppendText(txt + Environment.NewLine);
    }
    public void RunProcess()
    {
        string filePath = @"C:\Users\USerName\Documents\Visual Studio 2017\Projects\Aufgabe17NormalMapping\images.jpg";
        using (img = new Bitmap(filePath))
        {
            var height = img.Height;
            var width = img.Width;
            double[,] ArrayR = new double[width, height];
            double[,] ArrayG = new double[width, height];
            double[,] ArrayB = new double[width, height];


            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Color pixel = img.GetPixel(i, j);
                    ArrayR[i, j] = (((double)Convert.ToDouble(pixel.R.ToString())) + 0.5) * 2;
                    ArrayG[i, j] = (((double)Convert.ToDouble(pixel.G.ToString())) + 0.5) * 2;
                    ArrayB[i, j] = (((double)Convert.ToDouble(pixel.B.ToString())) + 0.5) * 2;
                    Debug.WriteLine(ArrayR[i, j].ToString(), "Array R");
                    //Debug.Print(ArrayR[i, j].ToString(), "Array Rsdsd");
                    BeginInvoke(TextHandler, ArrayR[i, j].ToString());


                }
            }

        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Task T = new Task(RunProcess);
        T.Start();
    }

}

}

1 个答案:

答案 0 :(得分:1)

你的代码非常快,它是&#34;报告&#34;这需要很长时间。删除Debug.WriteLineTextHandler,我得到以下时间(在调试模式下)

ImgSize 532x1280
Lasted: 0:00:01,181845

代码段:

static void Main(string[] args)
{
    var watch = Stopwatch.StartNew();
    RunProcess();
    Console.WriteLine($"Lasted: {watch.Elapsed.ToString("g")}");
}

public static void RunProcess()
{
    var str = new StringBuilder();
    string filePath = @"C:\Users\Wouter\Desktop\gamez.jpg";
    using (var img = new Bitmap(filePath))
    {
        var height = img.Height;
        var width = img.Width;
        Console.WriteLine($"ImgSize {width}x{height}");

        double[,] ArrayR = new double[width, height];
        double[,] ArrayG = new double[width, height];
        double[,] ArrayB = new double[width, height];

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                Color pixel = img.GetPixel(i, j);
                ArrayR[i, j] = (((double)Convert.ToDouble(pixel.R.ToString())) + 0.5) * 2;
                ArrayG[i, j] = (((double)Convert.ToDouble(pixel.G.ToString())) + 0.5) * 2;
                ArrayB[i, j] = (((double)Convert.ToDouble(pixel.B.ToString())) + 0.5) * 2;
                str.AppendLine(ArrayR[i, j].ToString());
            }
        }
    }
    BeginInvoke(TextHandler, str.ToString());
}

尝试使用StringBuilder,一旦处理完整个图片,请更新一次用户界面。