C#图片转成流,流转成图片,字节转图片,图片转字节的方法

图片转成流

Bitmap b = new Bitmap(Server.MapPath(ppath));
Stream ms = new MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

流转成图片

MemoryStream ms = new MemoryStream(Bytes);
return Bitmap.FromStream(ms,true);

字节转图片

/// <summary>
/// 字节数组生成图片
/// </summary>
/// <param name="Bytes">字节数组</param>
/// <returns>图片</returns>
private Image byteArrayToImage(byte[] Bytes)
{
    MemoryStream ms = new MemoryStream(Bytes);
    return Bitmap.FromStream(ms,true);
}

图片转字节

/// <summary>
/// 根据图片路径返回图片的字节流byte[]
/// </summary>
/// <param name="imagePath">图片路径</param>
/// <returns>返回的字节流</returns>
privatestaticbyte[] getImageByte(stringimagePath)
{
FileStream files = newFileStream(imagePath, FileMode.Open);
byte[] imgByte = newbyte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
returnimgByte;
}

 剪切图片与将剪切的图片生成内存流MemoryStream

int pw = 60;
int ph = 15;
int px =80;
int py = 100; 
ppath = @"d:\123.jpg";
Bitmap b = new Bitmap(ppath);
//剪裁图片
RectangleF rec = new RectangleF(px, py, pw, ph);
Bitmap nb = b.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//剪切后并读入到流中去
Stream ms = new MemoryStream();
nb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

 

posted @ 2017-07-18 09:57  君莫笑·秋  阅读(21830)  评论(0编辑  收藏  举报