简体   繁体   English

在C#中将位图从16bpp转换为8bpp

[英]Convert bitmap from 16bpp to 8bpp in C#

I have a Bitmap with 16bpp. 我有一个16bpp的位图。 I want to convert that image in my ASP.NET side in a 8bpp image. 我想将图像转换为8bpp图像的ASP.NET端。

I tried a lot of options which I found in the internet but nothing works for me. 我尝试了很多在Internet上找到的选项,但对我没有任何帮助。

I also tried that way: C# Converting 32bpp image to 8bpp 我也尝试过这种方式: C#将32bpp图像转换为8bpp

But if I want to save the file, I get the following error: 但是,如果要保存文件,则会出现以下错误:

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. 异常详细信息:System.Runtime.InteropServices.ExternalException:GDI +中发生一般错误。

Line 278:                    System.Drawing.Image img2 = Convert(bm_resize);//byteArrayToImage(gray);
Line 279:                    
Line 280:                    img2.Save(helper+"grey2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Line 281:                }
Line 282:

Is there any correct way for my problem? 有什么正确的方法可以解决我的问题吗?

Full Code: 完整代码:

System.Drawing.Image img2 = Convert(bm_resize);
img2.Save(path+"test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


public static System.Drawing.Image Convert(Bitmap oldbmp)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            oldbmp.Save(ms, ImageFormat.Gif);
            ms.Position = 0;
            return System.Drawing.Image.FromStream(ms);
        }
    }

The problem is caused by disposing the memory stream before the image is saved. 该问题是由于在保存图像之前处理内存流而引起的。

I believe GDI+ requires the memory stream to persist while you are still working with an Image created from the memory stream. 我相信GDI +要求在您仍在使用从该内存流创建的映像时继续使用该内存流。

See the Microsoft Support article. 请参阅Microsoft支持文章。

I use a little helper function to create 1 bpp monochrome bitmaps in .NET. 我使用一些辅助功能在.NET中创建1 bpp单色位图。 Check out this link, it also works great for 8 bpp 看看这个链接,它也非常适合8 bpp

http://www.wischik.com/lu/programmer/1bpp.html http://www.wischik.com/lu/programmer/1bpp.html

hope this could help! 希望这会有所帮助!

AForge.Net has a good collection of free (under Lesser GPL License) routines for such changes and a lot more. AForge.Net拥有大量免费的例程(根据Lesser GPL License),可以进行此类更改以及更多其他更改。 Conversion of 16bpp to 8bpp is as simple as this . 16bpp到8bpp的转换就这么简单。

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

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