编辑:实际上想到它,也许最好将缩略图保存在附加列中,所以现在我需要一种方法来压缩和调整图像大小,然后将它们保存到数据库很长时间,保存一个未受影响的副本.最初通过在httpPostedfileBase中传递图像来保存图像,现在需要一些在保存到数据库之前调整大小和压缩的工具.
public fileContentResult Getimage(int lineID){ var PMedia = repository.ProductMedias.FirstOrDefault(x => x.lineID == lineID); if (PMedia != null) { return file(PMedia.ImageData,PMedia.ImageMimeType,PMedia.filename); } else { return null; }}解决方法 这是我用来制作缩略图的例程:
public voID Makethumbnail(string imagePath) { // Image exists? if (string.IsNullOrEmpty(imagePath)) throw new fileNotFoundException("Image does not exist at " + imagePath); // Default values string filename = imagePath.Tolower().Replace(".jpg","_thumb.jpg"); int WIDth = 100; // 180; int Height = 75; // 135; bool lSaved = false; // Load image Bitmap bitmap = new Bitmap(imagePath); // If image is smaller than just save try { if (bitmap.WIDth <= WIDth && bitmap.Height <= Height) { bitmap.Save(filename,ImageFormat.Jpeg); lSaved = true; } } catch (Exception e) { throw new Exception(e.Message); } finally { bitmap.dispose(); } if (!lSaved) { Bitmap FinalBitmap = null; // Making Thumb try { bitmap = new Bitmap(imagePath); int BitmapNewWIDth; decimal Ratio; int BitmapNewHeight; // Change size of image Ratio = (decimal)WIDth / Height; BitmapNewWIDth = WIDth; BitmapNewHeight = Height; // Image processing FinalBitmap = new Bitmap(BitmapNewWIDth,BitmapNewHeight); Graphics graphics = Graphics.FromImage(FinalBitmap); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.FillRectangle(Brushes.White,BitmapNewWIDth,BitmapNewHeight); graphics.DrawImage(bitmap,BitmapNewHeight); // Save modifIEd image FinalBitmap.Save(filename,ImageFormat.Jpeg); } catch (Exception e) { throw new Exception(e.Message); } finally { if (FinalBitmap != null) FinalBitmap.dispose(); } } }总结
以上是内存溢出为你收集整理的c# – 需要一些能够创建缩略图的精简库/代码,并且最适合与ASP.NET MVC一起使用全部内容,希望文章能够帮你解决c# – 需要一些能够创建缩略图的精简库/代码,并且最适合与ASP.NET MVC一起使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)