转换一个24位的内存映像索引颜色 [英] Convert a 24 bit in memory image to indexed color

查看:262
本文介绍了转换一个24位的内存映像索引颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经在内存中Format24bppRgb创建的映像。

I have an image that I have created in memory as Format24bppRgb.

我保存这个为一个PNG,这是24KB。

I save this as a PNG and it is 24kb.

如果我用Photoshop保存相同的图像作为24位PNG谈到差不多大,但如果我把它保存为一个8位PNG只有32色就下降到5KB。

If I save the same image with photoshop as a 24bit PNG it comes to about the same size, but if i save it as an 8bit PNG with only 32 colors it goes down to 5kb.

如何创建一个PNG文件的索引版本使用C#/ GDI + / System.Drawing中?

How can I create an indexed version of a PNG file using C# / GDI+ / System.Drawing ?

我有一个很难找到任何答案,这个问题不只是适用于灰​​度图像或需要外部库。是不是GDI +中可能的是?

I'm having a hard time finding any answers to this question that don't just apply to grayscale images or require an external library. is it not possible within GDI+ as is?

推荐答案

我结束了使用 FreeImageAPI DLL ,与C#包装。我会尽量避免第三方库,但我不认为有任何其他的选择。这似乎是一个pretty的标准,很好的利用图书馆。

I ended up using the FreeImageAPI DLL, with a C# wrapper. I try to avoid 3rd party libraries but I don't think there is any other choice. It seems a pretty standard and well used library.

在我来说,我有一个为System.Drawing.Image 对象图像这是动态创建的 - 这我想保存到HttpContext的输出流作为一个COM pressed图像。需要注意的是图片不具备所需的 GetHbitmap()的方法,但位图一样。

In my case I have an System.Drawing.Image object 'image' that was dynamically created - which I want to save to the HttpContext output stream as a compressed image. Note that Image does not have the needed GetHbitmap() method, but Bitmap does.

// convert image to a 'FreeImageAPI' image
var fiBitmap = FreeImageAPI.FreeImageBitmap.FromHbitmap((image as Bitmap).GetHbitmap());
fiBitmap.ConvertColorDepth(FreeImageAPI.FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP);

// see http://stackoverflow.com/questions/582766
MemoryStream ms = new MemoryStream();
fiBitmap.Save(ms, FreeImageAPI.FREE_IMAGE_FORMAT.FIF_PNG, FreeImageAPI.FREE_IMAGE_SAVE_FLAGS.PNG_Z_BEST_COMPRESSION);
context.HttpContext.Response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);

我的24KB的位图,现在只有9KB: - )

My 24kb bitmap is now only 9kb :-)

提示:一定要兼得FreeImageNET',当你运行它在你的bin可用FreeImage的'的DLL。在目前的C#示例项目包含一个对Library.DLL,这似乎并不在他们的zip文件存在。使用 FreeImageNET.dll FreeImage.dll 作品虽然。你会得到一个恼人的文件中未发现的错误,如果你不知道, FreeImageNet != FreeImage的

Tip: Be sure to have both 'FreeImageNET' AND 'FreeImage' dlls available in your bin when you run it. At the current time the C# sample projects contain a reference to 'Library.DLL' which doesn't seem to exist in their zip file. Using FreeImageNET.dll and FreeImage.dll works though. You'll get an annoying file not found error if you don't realize that FreeImageNet != FreeImage !

这篇关于转换一个24位的内存映像索引颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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