- C# code
/// <summary> /// 图片缩放 /// </summary> /// <param name="sourcePath">客户端路径</param> /// <param name="savePath">保存服务器端路径</param> /// <param name="w">宽</param> /// <param name="h">高</param> public static void ImageChange(string sourcePath, string savePath, int w, int h) { System.Drawing.Image _sourceImg = System.Drawing.Image.FromFile(sourcePath); double _newW = (double)w, _newH = (double)h, t; if ((double)_sourceImg.Width > w) { t = (double)w; } else { t = (double)_sourceImg.Width; } if ((double)_sourceImg.Height * (double)t / (double)_sourceImg.Width > (double)h) { _newH = (double)h; _newW = (double)h / (double)_sourceImg.Height * (double)_sourceImg.Width; } else { _newW = t; _newH = (t / (double)_sourceImg.Width) * (double)_sourceImg.Height; } System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH); System.Drawing.Graphics g = Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel); _sourceImg.Dispose(); g.Dispose(); try { bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { } bitmap.Dispose(); }
这段图片缩放后再上传的代码(服务器上自保持缩放后的),我本机运行可用,为神马上传到服务器上就不可以呢?
------解决方案--------------------------------------------------------
<param name="sourcePath">客户端路径</param>
这个参数怎么能使用客户端路径呢?服务器上的代码是无法处理客户端上的文件的,必须上传再fileUpload.SaveAs()之后才能进行
------解决方案--------------------------------------------------------
这段代码没有实现上传功能,如引用所说。
在本机既是客户端又是服务端,所以貌似是可以上传的,其实所有的操作都是在服务器上实现的,你操作的所谓的本地路径,其实还是服务器的硬盘路径,你可以测试下……
1楼正确!!!
------解决方案--------------------------------------------------------
你上传成功后,然后生成缩略图后成功后在获取旧图片的地址,删除即可!
------解决方案--------------------------------------------------------