c#将所有图像格式转换为JPG [英] c# Converting all image formats to JPG

查看:240
本文介绍了c#将所有图像格式转换为JPG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for solution to "converting all image formats to 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#将图像格式转换为jpg

我编辑了一些功能,并添加了更多值.并将代码放入其中以供使用.

I edited some and added some more values to function. And put codes inside to using.

    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

推荐答案

也许此代码可以为您提供帮助:

Maybe this code will help you :

ImageFormat是System.Drawing.Imaging的一类

ImageFormat is a class of 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);
}

这篇关于c#将所有图像格式转换为JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆