下载方法
public static void downloadImage(string Imgurl, Dictionary<string, string> keyValues)
{
foreach (var item in keyValues)
{
try
{
Thread.Sleep(2);
System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
WebRequest request = WebRequest.Create(Imgurl);
WebResponse response = request.GetResponse();
Stream reader = response.GetResponseStream();
if (!Directory.Exists(item.Value))
{
Directory.CreateDirectory(item.Value);
}
FileStream writer = new FileStream(item.Value + item.Key, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buff = new byte[512];
int c = 0; //实际读取的字节数
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
writer.Write(buff, 0, c);
}
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("***************保存图片报错了,错误{0}****************", ex.Message);
}
}
}
调用方法
Dictionary<string, string> dir = new Dictionary<string, string>();
dir.Add(filename, filepath);//filename:保存在本地时的文件名,filepath保存在本地的地址
//imgpath:文件下载地址
downloadImage(imgpath, dir);
单个文件下载
bool downloadImage(string DownloadUrl, string SaveUrl, string FileName,bool isFace, out string msg)
{
try
{
string sourceFilename = "";
msg = "";
if(isFace)
{
//获取绝对路径
sourceFilename = base.Request.MapPath(SaveUrl) + "\\" + FileName;
Thread.Sleep(2);
System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
WebRequest request = WebRequest.Create(DownloadUrl);
WebResponse responses = request.GetResponse();
Stream reader = responses.GetResponseStream();//获取图片字节
if (!Directory.Exists(SaveUrl))
{
Directory.CreateDirectory(SaveUrl);
}
FileStream writer = new FileStream(sourceFilename, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buff = new byte[512];
int c = 0; //实际读取的字节数
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
writer.Write(buff, 0, c);
}
writer.Close();
writer.Dispose();
reader.Close();
reader.Dispose();
responses.Close();
}
}
修改图片比例 并保存到绝对路径
//原格式图片路径(可以为相对路径),修改比例后保存的路径(必须为绝对路径),宽度,高度
public static void CreateThumbnail(string sourceFilename, string destFilename, int width, int height)
{
Image image = Image.FromFile(sourceFilename);
if (image.Width <= width && image.Height <= height)
{
File.Copy(sourceFilename, destFilename, true);
image.Dispose();
}
else
{
int width2 = image.Width;
int height2 = image.Height;
float num = (float)height / (float)height2;
if ((float)width / (float)width2 < num)
{
num = (float)width / (float)width2;
}
width = (int)((float)width2 * num);
height = (int)((float)height2 * num);
Image image2 = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(image2);
graphics.Clear(Color.White);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width2, height2), GraphicsUnit.Pixel);
EncoderParameters encoderParameters = new EncoderParameters();
EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, 100L);
encoderParameters.Param[0] = encoderParameter;
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo encoder = null;
int num2 = 0;
while (num2 < imageEncoders.Length)
{
if (!imageEncoders[num2].FormatDescription.Equals("JPEG"))
{
num2++;
continue;
}
encoder = imageEncoders[num2];
break;
}
image2.Save(destFilename, encoder, encoderParameters);
encoderParameters.Dispose();
encoderParameter.Dispose();
image.Dispose();
image2.Dispose();
graphics.Dispose();
}
}
获取缩略图
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
{
ImageFormat rawFormat = b.RawFormat;
int num = 0;
int num2 = 0;
int width = b.Width;
int height = b.Height;
if (height > destHeight || width > destWidth)
{
if (width * destHeight < height * destWidth)
{
num = destWidth;
num2 = destWidth * height / width;
}
else
{
num2 = destHeight;
num = width * destHeight / height;
}
}
else
{
num = destWidth;
num2 = destHeight;
}
Bitmap bitmap = new Bitmap(destWidth, destHeight);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(b, new Rectangle((destWidth - num) / 2, (destHeight - num2) / 2, num, num2), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel);
graphics.Dispose();
EncoderParameters encoderParameters = new EncoderParameters();
long[] value = new long[1]
{
100L
};
EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, value);
encoderParameters.Param[0] = encoderParameter;
b.Dispose();
return bitmap;
}