10.8w+
社区成员
FileStream ReadStream = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[ReadStream.Length];
ReadStream.Read(data, 0, data.Length);
// 将读取的数据转为图片
MemoryStream bs = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(bs);
或者:pictureBox1.Image = new Bitmap(bs);
ReadStream.Close();
WriteStream.Close();
var ar = File.ReadAllBytes("原始二进制文件.raw");
int h = 1240;
int w = 1210;
bm = new Bitmap(w, h);
int i = 0;
for (var x = 0; x < w; x++)
{
for (var y = 0; y < h; y++)
{
var n = ar[i++];
bm.SetPixel(x, y, Color.FromArgb(n, n, n));
}
}
bm.Save("1,jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
private void button2_Click(object sender, EventArgs e)
{
var ar = File.ReadAllBytes("输入文件.raw");
int w = 1240;
int h = 1210;
Bitmap bitm = new Bitmap(w, h);
int i = 0;
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
var n = ar[i++];
bitm.SetPixel(x, y, Color.FromArgb(n, n, n));
}
}
pictureBox1.Image = bitm;
bitm.Save(@"输出文件.png", System.Drawing.Imaging.ImageFormat.Png);
public static Bitmap TifToBmp(int[][] TifData, int maxv, int minv)
{
int Width = TifData.Length;
int Height = TifData[1].Length;
Bitmap TifMap = new Bitmap(Width, Height);
int diff = maxv - minv;
Rectangle rect = new Rectangle(0, 0, TifMap.Width, TifMap.Height);
BitmapData srcBmpData = TifMap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr srcPtr = srcBmpData.Scan0;
int scanWidth = srcBmpData.Width * 3;
int src_bytes = scanWidth * TifMap.Height;
byte[] srcRGBValues = new byte[src_bytes];
Marshal.Copy(srcPtr, srcRGBValues, 0, src_bytes);
for (int j = 0; j < Height; j++)
{
for (int i = 0; i < Width; i++)
{
int colr = (int)((double)(TifData[i][j] - minv) * 256 / diff);
if (colr > 255)
colr = 255;
if (colr < 0)
colr = 0;
srcRGBValues[j * scanWidth + i * 3 + 2] = (byte)colr;
srcRGBValues[j * scanWidth + i * 3 + 1] = (byte)colr;
srcRGBValues[j * scanWidth + i * 3 + 0] = (byte)colr;
}
}
Marshal.Copy(srcRGBValues, 0, srcPtr, src_bytes);
TifMap.UnlockBits(srcBmpData);
return TifMap;
}
public class ReadTIF
{
public static int[][] ReadTifData(string FileName)
{
Bitmap bmp = Bitmap.FromFile(FileName) as Bitmap ;
FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
BinaryReader TIFReader = new BinaryReader(stream);
int Width = bmp.Width ;
int Height = bmp.Height ;
int colr = 0;
byte[] pixelData = new byte[2];
int[][] Imdata = new int[Width][];
for (int i = 0; i < Width; i++)
{
Imdata[i] = new int[Height];
}
for (int j = 0; j < Height; j++)
{
for (int i = 0; i < Width; i++)
{
pixelData = TIFReader.ReadBytes(2);
colr = (pixelData[1] * 256 + pixelData[0]);
Imdata[i][j] = colr;
}
}
stream.Close();
return Imdata;
}
public static int[][] ReadRawData(string FileName,int Width,int Height)
{
int[][] result = new int[Width][];
for(int i=0;i<Width;i++)
{
result[i] = new int[Height];
}
ushort[] pix16;
BinaryReader br = new BinaryReader(File.Open(FileName, FileMode.Open));
ushort pixShort;
long iTotalSize = br.BaseStream.Length;
int iNumberOfPixels = (int)(iTotalSize / 2);
pix16 = new ushort[iNumberOfPixels];
for (int i = 0; i < iNumberOfPixels; ++i)
{
pixShort = (ushort)(br.ReadUInt16());
pix16[i] = pixShort;
}
for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
result[i][j]= (int)(pix16[i *Height + j]);
}
}
return result;
}
public static int[][] ReadRawData(string FileName, int Width, int Height,out int MaxValue,out int MinValue)
{
MaxValue = 0;
MinValue = 65535;
int[][] result = new int[Width][];
for (int i = 0; i < Width; i++)
{
result[i] = new int[Height];
}
ushort[] pix16;
BinaryReader br = new BinaryReader(File.Open(FileName, FileMode.Open));
ushort pixShort;
long iTotalSize = br.BaseStream.Length;
int iNumberOfPixels = (int)(iTotalSize / 2);
pix16 = new ushort[iNumberOfPixels];
for (int i = 0; i < iNumberOfPixels; ++i)
{
pixShort = (ushort)(br.ReadUInt16());
pix16[i] = pixShort;
}
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
result[i][j] = (int)(pix16[i * Height + j]);
if (MaxValue < result[i][j])
MaxValue = result[i][j];
if (MinValue > result[i][j])
MinValue = result[i][j];
}
}
return result;
}
}