把网络数据流转换成图片类


1
public void GetImageSize(string FullFileName, int intPrintImageHeight, int intPrintImageWidth, ref int intmageHeight, ref int intImageWidth)
2 {
3 //Dim oImage As Image = Nothing
4 //oImage = Image.FromFile(FullFileName.Replace("/", "\").Replace("http:\\", ""))
5
6 //Dim bytes As Byte() = New System.Net.WebClient().DownloadData("")
7  
8 WebRequest webRequest__1 = WebRequest.Create(FullFileName);
9 WebResponse webResponse = webRequest__1.GetResponse();
10 Bitmap oImage = new Bitmap(webResponse.GetResponseStream());
11 //intImageWidth = myImage.Width
12 //intmageHeight = myImage.Height
13
14 try {
15 if (oImage.Width > intPrintImageWidth && oImage.Height > intPrintImageHeight) {
16 if ((intPrintImageWidth / oImage.Width) < (intPrintImageHeight / oImage.Height)) {
17 intImageWidth = intPrintImageWidth;
18 intmageHeight = Conversion.Int(oImage.Height * intPrintImageWidth / oImage.Width);
19 } else {
20 intImageWidth = Conversion.Int(oImage.Width * intPrintImageHeight / oImage.Height);
21 intmageHeight = intPrintImageHeight;
22 }
23 } else if (oImage.Width > intPrintImageWidth) {
24 intImageWidth = intPrintImageWidth;
25 intmageHeight = Conversion.Int(oImage.Height * intPrintImageWidth / oImage.Width);
26
27 } else if (oImage.Height > intPrintImageHeight) {
28 intImageWidth = Conversion.Int(oImage.Width * intPrintImageHeight / oImage.Height);
29 intmageHeight = intPrintImageHeight;
30 } else {
31 intImageWidth = oImage.Width;
32 intmageHeight = oImage.Height;
33 }
34
35 } catch (Exception ex) {
36 throw (ex);
37 } finally {
38 if (oImage != null) {
39 oImage.Dispose();
40 oImage = null;
41 }
42 }
43
44
45 }



验证是否图片存在
public int GetUrlExistError(string url)
{
int num = 0;
if (url.Length > 1) {
HttpWebRequest request
= (HttpWebRequest)WebRequest.Create(new Uri(url));
ServicePointManager.Expect100Continue
= false;
try {
((HttpWebResponse)request.GetResponse()).Close();
}
catch (WebException exception) {
if (exception.Status != WebExceptionStatus.ProtocolError) {
return num;
}
if (exception.Message.IndexOf("500 ") > 0) {
return 500;
}
if (exception.Message.IndexOf("401 ") > 0) {
return 401;
}
if (exception.Message.IndexOf("404") > 0) {
return 404;
}
}
num
= 1;
}
return num;
}

原文地址:https://www.cnblogs.com/xiaobuild/p/2082795.html