C# 将字节数组转换为 PNG/JPG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8946846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:21:15  来源:igfitidea点击:

Converting a byte array to PNG/JPG

c#pngjpeg

提问by user472875

I am currently working on an application that requires high-performance conversion of an unpadded byte array to either a PNG or JPEG. The image format doesn't matter, just as long as it's fast.

我目前正在开发一个需要将未填充字节数组高性能转换为 PNG 或 JPEG 的应用程序。图像格式无所谓,只要速度快就行。

I have tried the .NET libraries and the performance is very bad. Can anyone recommend a good freeware library for this?

我尝试过 .NET 库,但性能很差。任何人都可以为此推荐一个好的免费软件库吗?

EDIT: the byte[] is an 8bit grayscale bitmap

编辑:字节 [] 是一个 8 位灰度位图

采纳答案by Garrett Vlieger

You should be able to do something like this:

你应该能够做这样的事情:

byte[] bitmap = GetYourImage();

using(Image image = Image.FromStream(new MemoryStream(bitmap)))
{
    image.Save("output.jpg", ImageFormat.Jpeg);  // Or Png
}

Look herefor more info.

看看这里获取更多信息。

Hopefully this helps.

希望这会有所帮助。

回答by ahoffer

回答by JayC

There are two problems with this question:

这个问题有两个问题:

Assuming you have a gray scale bitmap, you have two factors to consider:

假设您有一个灰度位图,您需要考虑两个因素:

  1. For JPGS... what loss of quality is tolerable?
  2. For pngs... what level of compression is tolerable? (Although for most things I've seen, you don't have that much of a choice, so this choice might be negligible.) For anybody thinking this question doesn't make sense: yes, you can change the amount of compression/number of passes attempted to compress; check out either Ifranview or some of it's plugins.
  1. 对于 JPGS... 质量损失是可以容忍的?
  2. 对于 png... 什么级别的压缩是可以容忍的?(虽然对于我见过的大多数事情,你没有那么多选择,所以这个选择可能可以忽略不计。)对于任何认为这个问题没有意义的人:是的,你可以改变压缩量/尝试压缩的传递次数;查看 Ifranview 或其中的一些插件。

Answer those questions, and then you might be able to find your original answer.

回答这些问题,然后您可能会找到原来的答案。