1、将原始图片下载下来: using System.Net;string url = "http://www.mozilla.org/images/feature-back-cnet.png"; WebClient myWebClient = new WebClient(); //将图片下载到 C:\temp\feature-back-cnet.png myWebClient.DownloadFile(url,"C:\temp\feature-back-cnet.png"); 2、将图片转换成100px*100px的图片 using System.Drawing;Image image = Image.FromFile("C:\temp\feature-back-cnet.png"); Bitmap bmp = new Bitmap(image,new Size(100,100)); 将新图片保存到 C:\temp\newPic.png bmp.Save("C:\temp\newPic.png",System.Drawing.Imaging.ImageFormat.Png);
pictrue_address=@http://www.mozilla.org/images/feature-back-cnet.png WebClient myWebClient = new WebClient();//以同名下载文件 int ind=pictrue_address.LastIndexOf ("/"); pictruename=pictrue_address.Substring(index); myWebClient.DownloadFile(pictrue_address,@"c\"+pictrue_address.Substring(ind)) string path=@"c:\"+pictrue_address.Substring(ind); Bitmap myBitmap = new Bitmap(path); Image f=myBitmap.GetThumbnailImage (100,100,new Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero); f.Save (pictrue_address.Substring(ind));public bool ThumbnailCallback() { return true; }
实现缩略图: System.Drawing.Image image = System.Drawing.Image.FromStreamthis.htlFileName.PostedFile.InputStream);(这句可以换掉,只要获取到一个image对象就可以了) int width=1000;(你要的宽度) int height=1000;(你要的高度) image=image.GetThumbnailImage(width,height,null,IntPtr.Zero);(返回新的image对象) 后面是保存方法, System.IO.FileStream stream = new FileStream(baseLocation+fileName,System.IO.FileMode.Create,System.IO.FileAccess.Write); image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] b = new byte[stream.Length]; stream.Write(b,0,(int)stream.Length); stream.Close(); this.htlFileName.PostedFile.SaveAs(baseLocation+"1"+fileName);
using System.Net;string url = "http://www.mozilla.org/images/feature-back-cnet.png";
WebClient myWebClient = new WebClient();
//将图片下载到 C:\temp\feature-back-cnet.png
myWebClient.DownloadFile(url,"C:\temp\feature-back-cnet.png"); 2、将图片转换成100px*100px的图片
using System.Drawing;Image image = Image.FromFile("C:\temp\feature-back-cnet.png");
Bitmap bmp = new Bitmap(image,new Size(100,100));
将新图片保存到 C:\temp\newPic.png
bmp.Save("C:\temp\newPic.png",System.Drawing.Imaging.ImageFormat.Png);
WebClient myWebClient = new WebClient();//以同名下载文件
int ind=pictrue_address.LastIndexOf ("/");
pictruename=pictrue_address.Substring(index);
myWebClient.DownloadFile(pictrue_address,@"c\"+pictrue_address.Substring(ind))
string path=@"c:\"+pictrue_address.Substring(ind);
Bitmap myBitmap = new Bitmap(path);
Image f=myBitmap.GetThumbnailImage (100,100,new Image.GetThumbnailImageAbort(ThumbnailCallback),IntPtr.Zero);
f.Save (pictrue_address.Substring(ind));public bool ThumbnailCallback()
{
return true;
}
System.Drawing.Image image = System.Drawing.Image.FromStreamthis.htlFileName.PostedFile.InputStream);(这句可以换掉,只要获取到一个image对象就可以了)
int width=1000;(你要的宽度)
int height=1000;(你要的高度)
image=image.GetThumbnailImage(width,height,null,IntPtr.Zero);(返回新的image对象)
后面是保存方法,
System.IO.FileStream stream = new FileStream(baseLocation+fileName,System.IO.FileMode.Create,System.IO.FileAccess.Write);
image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] b = new byte[stream.Length];
stream.Write(b,0,(int)stream.Length);
stream.Close();
this.htlFileName.PostedFile.SaveAs(baseLocation+"1"+fileName);
后面哪个Png如何实现动态变换啊,比如我下载的图片如果是Gif的,Png就变成Gif了
string extension = System.IO.Path.GetExtension(fileName);//获取扩展名System.Drawing.Imaging.ImageFormat imf;
...
switch(fileName.Replace(".","").ToLower())
{
case "png":
imf = System.Drawing.Imaging.ImageFormat.Png;
break;
case "gif":
imf = System.Drawing.Imaging.ImageFormat.Gif;
break;
...
}
...
bmp.Save(fileName,imf);