GIS笔记_普通tif文件转成array数组 c#

引用的库文件:

using System;
using System.Collections;

using System.Windows.Media.Imaging;
using System.Drawing;
using System.Drawing.Imaging;

主要功能函数 :

public static System.Array TifToArray(string tifPath)
{
	//tif -> bitmap
	Image tifImage = Image.FromFile(tifPath);
	Bitmap bitmap = new Bitmap(tifImage);
	int height = (int)bitmap.Height;
	int width = (int)bitmap.Width;

	System.Array pixelsArray = Array.CreateInstance(typeof(float), height, width);

	GrayBitmapData grayBitmapdata = new GrayBitmapData(bitmap);

	byte[,] bitData = null;
	bitData = grayBitmapdata.GetData();

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			int[] indices = { i, j };
			pixelsArray.SetValue(bitData[i, j], indices);
		}
	}

	return pixelsArray;
}

辅助类:

(此处假设输入的tif文件是32位的图像,format32bppArgbFlag)

class GrayBitmapData
{
	private byte[,] Data;
	private int Width;
	private int Height;
	public GrayBitmapData()
	{
		this.Width = 0;
		this.Height = 0;
		this.Data = null;
	}
	public  GrayBitmapData(Bitmap bmp)
	{
		//from Bitmap to BitmapData
		BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), 
									ImageLockMode.ReadOnly, bmp.PixelFormat);

		this.Width = bmpData.Width;
		this.Height = bmpData.Height;
		Data = new byte[Height, Width];
		int temp = 0;
		unsafe
		{
			byte* ptr = (byte*)bmpData.Scan0.ToPointer();
			for (int i = 0; i < Height; i++)
			{
				for (int j = 0; j < Width; j++)
				{
					temp = (int)(0.114 * (*ptr++)) + (int)(0.587 * (*ptr++)) + (int)(0.299 * (*ptr++));
					//skip the blank or unused space bits 
					ptr++;
					Data[i, j] = (byte)temp;
				}
				
				//skip the blank space bit
				//if (format24bppRgbFlag)     ptr += bmpData.Stride - Width * 3;
				//if (format32bppArgbFlag)    ptr += 0;     // += bmpData.Stride - Width * 4; // https://www.cnblogs.com/dearzhoubi/p/8553763.html
			}
		}
		bmp.UnlockBits(bmpData);
	}
	public byte [,] GetData()
	{
		return Data;
	}
}

其他可能有用函数:(Bitmap <= 相互转换=> BitmapSource)

public static System.Drawing.Bitmap ToBitmap(BitmapSource source)
{
	using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
	{
		BitmapEncoder encoder = new BmpBitmapEncoder();
		encoder.Frames.Add(BitmapFrame.Create(source));
		encoder.Save(ms);
		return new System.Drawing.Bitmap(ms);
	}
}

public static BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
{
	System.IntPtr hBitmap = bmp.GetHbitmap();
	try
	{
		return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, System.IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
	}
	finally
	{
		DeleteObject(hBitmap);
	}
}

[System.Runtime.InteropServices.DllImport("Gdi32.dll")]
private static extern bool DeleteObject(System.IntPtr hObject);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惊鸿一博

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值