一個原始二進制檔案,一個正常圖片檔案,利用C#語言,前者該如何轉為后者?
資料源鏈接(免費下載):https://download.csdn.net/download/Dust_Evc/16675902
在嘗試了網上介紹的方法后還是失敗(主體如下代碼),故懇請各位大佬幫助!
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();
uj5u.com熱心網友回復:
無法下載,提示需要下載碼,你放百度網盤吧uj5u.com熱心網友回復:
鏈接:https://pan.baidu.com/s/1Ae5oAo4t7nyOIAEGZbBXnQ提取碼:9958
好的,第一次在論壇提問,感謝大神幫助!
uj5u.com熱心網友回復:
您好!已貼出百度網盤鏈接
uj5u.com熱心網友回復:
首先你那個raw檔案,用raw查看器打不開,誰知道是啥格式。好吧,就算是raw格式的吧,用這個
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;
}
}
uj5u.com熱心網友回復:
接上面
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 int Maxv =65535;
public int Minv = 0;
TifData = ReadTIF.ReadRawData(open.FileName, ImgWidth寬, ImgHeight高, out Maxv,out Minv);
Pic_Show.BackgroundImage = ImageProcess.TifToBmp(TifData, Maxv, Minv);
uj5u.com熱心網友回復:
tzbh...... 你這是 CAD 的加密資料檔案轉載請註明出處,本文鏈接:https://www.uj5u.com/net/276488.html
標籤:C#
上一篇:AE 要素插值
