【问题标题】:c# Converting all image formats to JPG [closed]c#将所有图像格式转换为JPG [关闭]
【发布时间】:2013-09-23 07:08:55
【问题描述】:

我正在寻找“将所有图像格式转换为 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

【问题讨论】:

  • 该代码与...有什么关系?它不进行任何类型的转换。您对问题的 conversion 部分进行了哪些尝试?另外,你为什么关心一切都是JPG?其他几种格式也适用于网络。如果它们必须是 JPG,为什么不强制上传?
  • 亲爱的 debracey,我想将所有图片转换为 jpg,因为我只想知道用户上传的所有图片,JPG 格式。我是新手。顺便说一句@BartoszKP,感谢您的编辑。问候
  • 嗨 rene,感谢您的回复。我不想使用库进行转换。我认为图书馆==额外的负载。但我要试试。谢谢。问候
  • 重命名而不是转换。

标签: c# asp.net


【解决方案1】:

也许这段代码会对你有所帮助:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-30
    • 2023-03-14
    • 1970-01-01
    • 2013-01-11
    • 2023-03-05
    • 2015-03-12
    • 2018-08-26
    相关资源
    最近更新 更多