#region 返回图片的字节流byte[]
/// <summary>
/// 返回图片的字节流byte[]
/// </summary>
///<param name="imagepath">
///<param name="webclient">
/// <returns></returns>
public static byte[] getimagebyte(string imagepath, webclient webclient)
{
byte[] imgbyte = null;
try
{
//messagebox.show("getimagebyte");
//stopwatch stopwatch = new stopwatch();
//stopwatch.start();
//datetime datestart = datetime.now;
if (pubfunc.urldiscern(imagepath))
{
bitmap bt = new bitmap(webclient.openread(imagepath));
imgbyte = pubfunc.imgtobyte(bt);
}
else
{
using (filestream files = new filestream(imagepath, filemode.open))
{
imgbyte = new byte[files.length];
files.read(imgbyte, 0, imgbyte.length);
files.close();
}
}
//stopwatch.stop();
//messagebox.show((datetime.now - datestart).totalmilliseconds.tostring());
}
catch (exception ee)
{
messagebox.show(ee.tostring());
}
return imgbyte;
}
#endregion
#region 图片转换成字节流
/// <summary>
/// 图片转换成字节流
/// </summary>
///<param name="img">要转换的image对象
/// <returns>转换后返回的字节流</returns>
public static byte[] imgtobyte(image img)
{
try
{
using (memorystream ms = new memorystream())
{
byte[] imagedata = null;
img.save(ms, system.drawing.imaging.imageformat.jpeg);
imagedata = ms.getbuffer();
return imagedata;
}
}
catch (exception ee)
{
messagebox.show(ee.tostring());
return null;
}
}
#endregion
以上就是c# 返回图片的字节流byte[]的内容。