C#对图片的操作

1、根据图片路径返回字节

public static byte[] getImageByte(string imagePath)
{
    FileStream files = new FileStream(imagePath, FileMode.Open);
    byte[] imgByte = new byte[files.Length];
    files.Read(imgByte, 0, imgByte.Length);
    files.Close();
    return imgByte;
}
public static byte[] ImgToByt(Image img)
{
    MemoryStream ms = new MemoryStream();
    byte[] imagedata = null;
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    imagedata = ms.GetBuffer();
    return imagedata;
}

2、字节转图片

public static Image BytToImg(byte[] byt)
{
    MemoryStream ms = new MemoryStream(byt);
    Image img = Image.FromStream(ms);
    return img;
}

 

posted @ 2015-06-15 14:14  梦锁湘江  阅读(507)  评论(0编辑  收藏  举报