如何将.bmp,.png文件转换为mvc4中的.jpeg文件 [英] How to convert .bmp,.png files to .jpeg files in mvc4
本文介绍了如何将.bmp,.png文件转换为mvc4中的.jpeg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
我在mvc4中使用fileupload控件......我需要的是我应该转换所有.bmp图像文件,.png图像文件,.gif图像文件等.......所有到.Jpeg文件..并转换为.jpeg文件,如果它小于4 MB我应该将其保存到数据库/应用程序路径........当它超过4 MB我应该压缩它并将其保存为.jpeg文件......任何人都可以给我一个样本
Hi Friends,
I am using a fileupload control in mvc4 ...... What i need is that i should convert all .bmp image files , .png image files ,.gif image files etc ....... all to .Jpeg Files .. And After converting to .jpeg files if it is less than 4 MB i should save it into database/Application path ........ And when it is more than 4 mb i should compress it and save it as .jpeg files ...... can anyone give me a sample
推荐答案
你可以尝试这样做将图像压缩为特定大小
You can try this so compress image into specific size
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
string path = Path.GetFileName(file.FileName) + ".jpeg";
string saveFilePath = Path.Combine(Server.MapPath("/Images/"), path);
// Resize Image
if ((file.ContentLength / 1024) > 4096)
{
var newSize = new Size(100, 100);
using (var originalImage = System.Drawing.Image.FromFile(file.FileName))
{
using (var newImage = new Bitmap(newSize.Width, newSize.Height))
{
using (var canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(originalImage, new System.Drawing.Rectangle(new Point(0, 0), newSize));
newImage.Save(saveFilePath, originalImage.RawFormat);
}
}
}
}
else
{
file.SaveAs(saveFilePath);
}
}
}
这篇关于如何将.bmp,.png文件转换为mvc4中的.jpeg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文