string imgUrl = "http://www.scapex.cn/upload/201601/28/201601280915393586.jpg";
if (!string.IsNullOrEmpty(imgUrl))
{
WebRequest webRequest = WebRequest.Create(new Uri(imgUrl));
WebResponse webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
string str = new System.Text.UnicodeEncoding().GetString(bytes);
stream.Close();
webResponse.Close();
Bitmap myImage = new Bitmap(webResponse.GetResponseStream());
MemoryStream ms = new MemoryStream();
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
读取文件转换成BaseCode
if (!File.Exists(filePath)) return baseCode;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
baseCode = Convert.ToBase64String(bytes);
}
BaseCode保存文件
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
byte[] bytes = Convert.FromBase64String(model.BaseCode);
using (Stream fs = new FileStream(model.FilePath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
public static string GetBaseCode(string filePath)
{
string base64 = string.Empty;
if (string.IsNullOrEmpty(filePath)) return base64;
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(filePath);
HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse();
try
{
using (Stream stream = response.GetResponseStream())
{
using (BinaryReader reader = new BinaryReader(stream))
{
byte[] arr = reader.ReadBytes(1024 * 500);
base64 = Convert.ToBase64String(arr);
}
}
response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
response.Dispose();
}
return base64;
}