如何在ASP.NET中将位图另存为16色灰度GIF或PNG? [英] How do I save a bitmap as a 16-color grayscale GIF or PNG in ASP.NET?
问题描述
在ASP.NET C#中,我试图将位图图像另存为16色非透明灰度图像,并另存为PNG或GIF.我假设我必须创建一个调色板,然后以某种方式将调色板附加到图像上,但不确定如何执行此操作.
In ASP.NET C# I'm trying to save a bitmap image as an 16-color non-transparent grayscale image as either a PNG or GIF. I assume I have to create a palette and then somehow attach the palette to the image but not sure how to go about doing this.
源图像是24位彩色位图.
The source image is a 24-bit color Bitmap.
推荐答案
这称为量化,而且很复杂.我对此问题进行了广泛的研究,而我的最佳结果是使用Octree量化和自定义扩散算法.
It's called quantization, and it's complicated. I've worked extensively with this problem, and my best results have been using Octree quantization and a custom diffusion algorithm.
从A到B的最快点是抓住我的代码(开源,但要下载$ 69),并使用极其简单的API将颜色计数设置为16,然后另存为GIF或PNG.如果要通过后台代码进行操作,应该大约两行代码...或者,如果它在文件系统上,则可以使用查询字符串:
Your fastest point from A to B is grab my code (open-source, but $69 to download) and use the extremely simple API to set the color count to 16 and save as GIF or PNG. Should be about 2 lines of code if you want to do it via code-behind... or, you can use a querystring if it's on the filesystem:
image.bmp?format=gif&colors=16
如果图像还没有灰度,则可以使用模块的ImageAttributes类来实现.生成的GIF将自动具有灰度调色板.最少的工作,很好的结果.
If the image isn't already grayscale, you can do that using the ImageAttributes class of the module. The resulting GIF will automatically have a grayscale palette. Minimal work, great results.
请记住,您不必将其用作HttpModule-它主要是用于调整图像大小,修改和编码的库.
Remember you don't have to use it as an HttpModule - it's primarily a library for resizing, modifying, and encoding images.
If you want to roll your own, here's what I started with: http://codebetter.com/blogs/brendan.tompkins/archive/2007/06/14/gif-image-color-quantizer-now-with-safe-goodness.aspx
通读注释,并根据我的注释修补指针算术错误....
Read through the comments and patch the pointer arithmetic errors per my comments....
但是,不要抖动,并且在不到完全信任的环境中运行原件可能会遇到麻烦.这些年来,我做了很多补丁,但我都不记得全部.
No dithering, though, and you may have trouble running the original in less than a full trust environment. I've made a lot of patches over the years, and I don't remember them all.
这篇关于如何在ASP.NET中将位图另存为16色灰度GIF或PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!