110,533
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将一组一维的字节数组转为图像。
/// </summary>
/// <exception cref="ArgumentNullException">Bytes 为 null 引用(在 Visual Basic 中为 Nothing)。</exception>
/// <exception cref="ArgumentException">Bytes 不包含图像数据。</exception>
/// <param name="Bytes">要转为图像的一组一维的字节数组。</param>
/// <returns>转换后的图像对象。</returns>
static public Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap(stream);
}
catch (ArgumentNullException ex)
{
throw ex;
}
catch (ArgumentException ex)
{
throw ex;
}
finally
{
stream.Close();
}
}
byte[] rawData;
GCHandle gch = GCHandle.Alloc(raw, GCHandleType.Pinned);
IntPtr scan0 = gch.AddrOfPinnedObject();
Bitmap bmp = new Bitmap(128, 128, 128 * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, scan0);
gch.Free();
PictureBox p;
Bitmap bmp;
using (Graphics g = p.CreateGraphics())
{
g.DrawImage(bmp, new Point(0, 0)); // =StretchDIBits
}
// 更简单的
p.Image = bmp;