c#将所有图像格式转换为JPG

时间:2013-09-22 20:42:32

标签: c# asp.net

我正在寻找“将所有图像格式转换为JPG”的解决方案。

我目前的代码:

    public ActionResult UploadProfilFotografi(HttpPostedFileBase file)
    {
        int sessionUID = int.Parse(Session["UID"].ToString());
        using (var dbContext = new DB_EMafya())
        {
            if (file != null
                && file.ContentLength > 1
                && file.ContentLength < 5120
                && myFunction.IsImage(file))
            {
                var users = dbContext
                    .tbl_Users
                    .FirstOrDefault(a => a.UID == sessionUID);

                if (users != null)
                {
                    string newPicName = (string) Session["UID"];
                    string extension = Path.GetExtension(file.FileName);
                    if (extension != null)
                    {
                        string picext = extension.ToLower();
                        string originalpath = Path.Combine(
                            Server.MapPath("~/up/profile-pictures/originals"),
                            newPicName + picext);

                        // file is uploaded
                        file.SaveAs(originalpath);
                    }
                }
            }
        }
        return RedirectToAction("ProfilAyarlari", "Profil");
    }

请帮忙吗?

我找到了解决方案: c# convert image formats to jpg

我编辑了一些并添加了一些更多的值来运行。并将代码放入使用。

    private void SaveAsJpgWithVaryQualityLevel(HttpPostedFileBase file, string toPath, string fileName)
    {
        using (var target = new MemoryStream())
        {
            file.InputStream.CopyTo(target);
            using (var bmp1 = new Bitmap(target)) // Get a bitmap.
            {
                var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                var myEncoder = Encoder.Quality;
                using (var myEncoderParameters = new EncoderParameters(1))
                {
                    using (var myEncoderParameter = new EncoderParameter(myEncoder, 100L))
                    {
                        myEncoderParameters.Param[0] = myEncoderParameter;
                        bmp1.Save(@"" + toPath + fileName, jgpEncoder, myEncoderParameters);
                    }
                }
            }
        }

    }

    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        return codecs.FirstOrDefault(codec => codec.FormatID == format.Guid);
    }

调用

string newPicName = Convert.ToString(Session["UID"]) + ".jpg";
string toPath = Server.MapPath("~/_up/profile-pictures/originals/");
SaveAsJpgWithVaryQualityLevel(file, toPath, newPicName); // file is uploaded

1 个答案:

答案 0 :(得分:4)

也许这段代码可以帮到你:

ImageFormat是System.Drawing.Imaging的一类

public Image BitmapToBytes(HttpPostedFileBase file, ImageFormat p_Format)
{

BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.InputStream.Length);

Image imageObject =  new Bitmap(MemoryStream(binData));

MemoryStream stream = new MemoryStream();
imageObject.Save(stream, p_Format);

return new Bitmap(stream);
}