如何在C#中将位图转换为2D整数数组,反之亦然? [英] How can I convert a bitmap to a 2D array of integers and vice versa in C#?

查看:131
本文介绍了如何在C#中将位图转换为2D整数数组,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Bitmap图像转换为2D整数数组,反之亦然。



我写了给定的代码。



这是我的驱动程序:



  private   void  forwardFftButton_Click( object  sender,EventArgs e)
{
Bitmap grayscale =(Bitmap)GrayscaleUtils.ColorToGrayscale(inputImagePictureBox.Image);

grayscaleImagePictureBox.Image = grayscale;

int [,] array2D = ImageDataConverter.BitmapToArray2D(灰度);

位图grayScaleAgain = ImageDataConverter.Array2DToBitmap(array2D);

processedImagePictureBox.Image = grayScaleAgain;
}









结果如下: [ ^ ]



转换无效。



我的代码中有什么错误?



我尝试过的方法:



 使用系统; 
使用 System.Drawing;
使用 System.Drawing.Imaging;

public partial class ImageDataConverter
{
public static int [,] BitmapToArray2D(位图图片)
{
int [,] array2D = ;
if (image.PixelFormat == PixelFormat.Format8bppIndexed)
{
array2D = new int [image.Width,image.Height];
BitmapData bitmapData = image.LockBits( new 矩形( 0 0 ,image.Width,image.Height),ImageLockMode.ReadOnly,PixelFormat.Format8bppIndexed);
int paddingOffset = bitmapData.Stride - (bitmapData.Width * 1 ); // 1 ==每像素1字节
不安全
{
byte * memoryAddress =( byte *)bitmapData.Scan0;
for int i = 0 ; i < bitmapData.Width; i ++)
{
for int j = 0 ; j < bitmapData.Height; j ++)
{
byte tempByte = memoryAddress [ 0 ]。
array2D [i,j] =( int )tempByte;
int tempArrayElement = array2D [i,j];
// 每像素1字节
memoryAddress + = 1 ;
}

memoryAddress + = paddingOffset;
}
}

image.UnlockBits(bitmapData);
}
其他
{
throw new 异常( 需要8位/像素索引图像。< /跨度>);
}

return array2D;
}

public static 位图Array2DToBitmap( int [,] image)
{
位图输出= 位图(image.GetLength(< span class =code-digit> 0
),image.GetLength( 1 ));
BitmapData bitmapData = output.LockBits( new Rectangle( 0 0 ,image.GetLength( 0 ),image.GetLength( 1 )),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
int paddingOffset = bitmapData.Stride - (bitmapData.Width * 1 ); // 1 ==每像素1字节
不安全
{
byte * memoryAddress =( byte *)bitmapData.Scan0;
for int i = 0 ; i < bitmapData.Height; i ++)
{
for int j = 0 ; j < bitmapData.Width; j ++)
{
int tempInt = image [j,i];
memoryAddress [ 0 ] =( byte )tempInt;
byte tempByte = memoryAddress [ 0 ];
// 每像素1字节
memoryAddress + = 1 ;
}

memoryAddress + = paddingOffset;
}
}

output.UnlockBits(bitmapData);
return 输出;
}
}

解决方案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



确保您没有在类型转换中丢失数据。

确保不要跳过数据。

确保bitmapdata符合您的期望第

I want to convert a Bitmap image into a 2D array of integers and vice versa.

I have written the given code.

And, this is my driver program:

private void forwardFftButton_Click(object sender, EventArgs e)
        {
            Bitmap grayscale = (Bitmap) GrayscaleUtils.ColorToGrayscale(inputImagePictureBox.Image);

            grayscaleImagePictureBox.Image = grayscale;

            int[,] array2D = ImageDataConverter.BitmapToArray2D(grayscale);

            Bitmap grayScaleAgain = ImageDataConverter.Array2DToBitmap(array2D);

            processedImagePictureBox.Image = grayScaleAgain;
        }





This is the result:[^]

The conversion isn't working.

What is the bug in my code?

What I have tried:

using System;
using System.Drawing;
using System.Drawing.Imaging;

public partial class ImageDataConverter
{
	public static int[, ] BitmapToArray2D(Bitmap image)
	{
		int[, ] array2D = null;
		if (image.PixelFormat == PixelFormat.Format8bppIndexed)
		{
			array2D = new int[image.Width, image.Height];
			BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
			int paddingOffset = bitmapData.Stride - (bitmapData.Width * 1); //1 == 1-byte per pixel
			unsafe
			{
				byte *memoryAddress = (byte *)bitmapData.Scan0;
				for (int i = 0; i < bitmapData.Width; i++)
				{
					for (int j = 0; j < bitmapData.Height; j++)
					{
						byte tempByte = memoryAddress[0];
						array2D[i, j] = (int)tempByte;
						int tempArrayElement = array2D[i, j];
						//1-byte per pixel
						memoryAddress += 1;
					}

					memoryAddress += paddingOffset;
				}
			}

			image.UnlockBits(bitmapData);
		}
		else
		{
			throw new Exception("8 bit/pixel indexed image required.");
		}

		return array2D;
	}

	public static Bitmap Array2DToBitmap(int[, ] image)
	{
		Bitmap output = new Bitmap(image.GetLength(0), image.GetLength(1));
		BitmapData bitmapData = output.LockBits(new Rectangle(0, 0, image.GetLength(0), image.GetLength(1)), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
		int paddingOffset = bitmapData.Stride - (bitmapData.Width * 1); //1 == 1-byte per pixel
		unsafe
		{
			byte *memoryAddress = (byte *)bitmapData.Scan0;
			for (int i = 0; i < bitmapData.Height; i++)
			{
				for (int j = 0; j < bitmapData.Width; j++)
				{
					int tempInt = image[j, i];
					memoryAddress[0] = (byte)tempInt;
					byte tempByte = memoryAddress[0];
					//1-byte per pixel
					memoryAddress += 1;
				}

				memoryAddress += paddingOffset;
			}
		}

		output.UnlockBits(bitmapData);
		return output;
	}
}

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Make sure you don't loose data in type conversion.
Make sure you don't skip data.
Make sure the bitmapdata match your expectations.


这篇关于如何在C#中将位图转换为2D整数数组,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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