110,534
社区成员
发帖
与我相关
我的任务
分享
string path = "C:\\test.dat";
byte[] byteArray = null;
if (File.Exists(path))
{
byteArray = File.ReadAllBytes(path);
}
BitmapImage item = new BitmapImage();
MemoryStream ms = new MemoryStream(byteArray);
item.BeginInit();
item.StreamSource = ms;
item.CacheOption = BitmapCacheOption.OnLoad;
item.CreateOptions = BitmapCreateOptions.None;
//item.EndInit();
item.Freeze();
ms.Dispose();
JpegBitmapEncoder en = new JpegBitmapEncoder();
en.Frames.Add(BitmapFrame.Create(item));
FileStream fstream = new FileStream("C:\\Users\\Sai\\Desktop\\sj.jpg",
FileMode.Create, FileAccess.ReadWrite);
en.Save(fstream);
fstream.Close();
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
struct BITMAPFILEHEADER
{
public UInt16 bfType;
public UInt32 bfSize;
public UInt16 bfReserved1;
public UInt16 bfReserved2;
public UInt32 bfOffBits;
}
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
struct BITMAPINFOHEADER
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public UInt16 biPlanes;
public UInt16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
struct BITMAPFILE
{
public BITMAPFILEHEADER file;
public BITMAPINFOHEADER info;
}
// 保存一个 大小 16*16 颜色格式rgb565 的16bpp原始数据图(纯红色)
static void saveADemo()
{
FileStream fs = new FileStream ("/Users/leendx/Desktop/demo.dat",FileMode.Create);
byte[] colorBytes = {0xf8,0x00}; // 1个纯红像素
for (int i = 0; i < 16*16; i++) {
fs.Write (colorBytes,0,2);
}
fs.Close ();
}
// 将示例的原始数据转为32位位图
static void dataTo32BMP()
{
FileStream fs = new FileStream ("/Users/leendx/Desktop/demo.dat",FileMode.Open);
byte[] colorBytes = new byte[16*16*2];
fs.Read (colorBytes, 0, 16 * 16 * 2);
byte[] bmpBytes32 = getBGRAFrom16bpp565 (colorBytes);
saveBMP_bgra ("/Users/leendx/Desktop/demo.bmp",16,16, bmpBytes32);
}
// 将16bpp 565原始数据 转换为 32bpp bgra
static byte[] getBGRAFrom16bpp565(byte[] src)
{
List<byte> lstResult = new List<byte>();
for (int i = 0; i < src.Length; i+=2) {
byte one = src[i];
byte two = src[i+1];
lstResult.Add((byte)(two & 0x1F <<3)); // b
lstResult.Add((byte)((two >> 5)&(one << 3) <<2)); // g
lstResult.Add((byte)(one >> 3 << 3)); // r
lstResult.Add(0xFF); // a
}
return lstResult.ToArray();
}
// 保存bgra位图
static void saveBMP_bgra(string fileName, int width, int height, byte[] data)
{
// 写文件头
uint bitmapFileStructSize = Convert.ToUInt32(Marshal.SizeOf(typeof(BITMAPFILE)));
uint dataBytes = Convert.ToUInt32(width * height << 2);
BITMAPFILE bf = new BITMAPFILE();
bf.file.bfType = 0x4D42;
bf.file.bfSize = bitmapFileStructSize + dataBytes;
bf.file.bfOffBits = bitmapFileStructSize;
bf.info.biSize = Convert.ToUInt32(Marshal.SizeOf(typeof(BITMAPINFOHEADER)));
bf.info.biWidth = width;
bf.info.biHeight = -height;
bf.info.biPlanes = 1;
bf.info.biBitCount = 32;
bf.info.biSizeImage = dataBytes;
// 获得文件头bytes
byte[] bmpFileHead = new byte[bitmapFileStructSize];
IntPtr pHead = Marshal.AllocHGlobal((int)bitmapFileStructSize);
Marshal.StructureToPtr (bf, pHead, false);
Marshal.Copy (pHead, bmpFileHead, 0, bmpFileHead.Length);
Marshal.FreeHGlobal (pHead);
// 将文件头及图像数据写入文件
FileStream fs = new FileStream (fileName,FileMode.Create);
fs.Write(bmpFileHead,0,bmpFileHead.Length);
fs.Write(data,0,data.Length);
fs.Close ();
}
public static void Main (string[] args)
{
saveADemo ();
dataTo32BMP ();
}