有没有办法安全地进行图像量化并且没有编组?

我目前正在使用Brendan Tompkins ImageQuantization dll。 http://codebetter.com/blogs/brendan.tompkins/archive/2007/06/14/gif-image-color-quantizer-now-with-safe-goodness.aspx

但它并没有在asp.net的中等信任中运行。

有谁知道一个以中等信任方式运行的图像量化库?

更新我不在乎解决方案是否缓慢。 我只需要一些有用的东西。

您应该能够使用Marshal替换代码,并通过BinaryReader等显式读取底层流。 这可能会慢一些,因为您必须将流完全读入您的托管内存或搜索它,而不是依赖已经非快速访问的非托管内存中的副本,但从根本上说这是您唯一的选择。

即使只执行读取操作,您也无法从中等信任上下文中窥探非托管内存。

查看链接代码之后,您就不允许这样做了。 对于初学者来说,他忽略了IntPtr的64 / 32bit方面!

他正在使用的基础BitMapData类完全取决于对任意内存的自由读取访问权限,这绝不会在中等信任下发生。
需要对其基本function进行重大改写才能直接使用BitMap(使用慢速GetPixel调用)或直接通过常规流apis读取数据,将其放入数组中,然后自行解析。 这些都不可能令人愉快。 前者会慢得多(我会期望由于每像素读取的高开销导致数量级),后者慢得多(虽然仍然较慢),但在重写图像数据的低级解析方面有更多相关的努力。

以下是基于当前代码需要更改的内容的粗略指南:

来自Quantizer.cs

public Bitmap Quantize(Image source) { // Get the size of the source image int height = source.Height; int width = source.Width; // And construct a rectangle from these dimensions Rectangle bounds = new Rectangle(0, 0, width, height); // First off take a 32bpp copy of the image Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb); // And construct an 8bpp version Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed); // Now lock the bitmap into memory using (Graphics g = Graphics.FromImage(copy)) { g.PageUnit = GraphicsUnit.Pixel; // Draw the source image onto the copy bitmap, // which will effect a widening as appropriate. g.DrawImage(source, bounds); } //!! BEGIN CHANGES - no locking here //!! simply use copy not a pointer to it //!! you could also simply write directly to a buffer then make the final immage in one go but I don't bother here // Call the FirstPass function if not a single pass algorithm. // For something like an octree quantizer, this will run through // all image pixels, build a data structure, and create a palette. if (!_singlePass) FirstPass(copy, width, height); // Then set the color palette on the output bitmap. I'm passing in the current palette // as there's no way to construct a new, empty palette. output.Palette = GetPalette(output.Palette); // Then call the second pass which actually does the conversion SecondPass(copy, output, width, height, bounds); //!! END CHANGES // Last but not least, return the output bitmap return output; } //!! Completely changed, note that I assume all the code is changed to just use Color rather than Color32 protected virtual void FirstPass(Bitmap source, int width, int height) { // Loop through each row for (int row = 0; row < height; row++) { // And loop through each column for (int col = 0; col < width; col++) { InitialQuantizePixel(source.GetPixel(col, row)); } // Now I have the pixel, call the FirstPassQuantize function... } } 

你需要在其他function中大致相同。 这消除了对Color32的任何需求,Bitmap类将为您处理所有这些。

Bitmap.SetPixel()将处理第二遍。 请注意,这是移植内容的最简单方法,但绝对不是在中等信任环境中执行此操作的最快方法。