简体   繁体   English

将任何图像格式转换为JPG

[英]Convert any image format to JPG

I have a web app that runs on Azure. 我有一个在Azure上运行的Web应用程序。 The user can upload an image and I save that image for them. 用户可以上传图片,我会为他们保存该图片。 Now when I receive the image, I have it as byte[] . 现在,当我收到图像时,将其作为byte[] I would like to save it as JPG to save space. 我想将其另存为JPG以节省空间。 The source image could be anything such as PNG, BMP or JPG. 源图像可以是诸如PNG,BMP或JPG之类的任何图像。

Is it possible to do so? 有可能这样做吗? This needs to run on Azure and I am using WebApps/MVC5/C#. 这需要在Azure上运行,而我正在使用WebApps / MVC5 / C#。

Thanks for any help. 谢谢你的帮助。

So the answers by @mjwills and Cody was correct. 因此,@ mjwills和Cody的回答是正确的。 I still thought to put the two methods I exactly needed: 我仍然想提出我确切需要的两种方法:

    public static Image BitmapToBytes(byte[] image, ImageFormat pFormat)
    {
        var imageObject = new Bitmap(new MemoryStream(image));

        var stream = new MemoryStream();
        imageObject.Save(stream, pFormat);

        return new Bitmap(stream);
    }

Also: 也:

    public static byte[] ImgToByteArray(Image img)
    {
        using (MemoryStream mStream = new MemoryStream())
        {
            img.Save(mStream, img.RawFormat);
            return mStream.ToArray();
        }
    }

Cheers everyone. 欢呼大家。

Get the memorystream and then use System.Drawing 获取内存流,然后使用System.Drawing

var stream = new MemoryStream(byteArray)
Image img =  new Bitmap(stream);
img.Save(@"c:\s\pic.png", System.Drawing.Imaging.ImageFormat.Png);

The last line there where you save the file, you can select the format. 保存文件的最后一行,您可以选择格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM