在 C#/GDI 中从 Format8bppIndexed 转换为 Format24bppRgb;

发布于 2024-07-15 01:13:19 字数 519 浏览 2 评论 0原文

好吧,我有一个来自外部应用程序的 8 位索引格式的图像。 我需要将此图像转换为大小完全相同的 24 位格式。

我尝试创建一个相同大小和 Format24bppRgb 类型的新位图,然后使用 Graphics 对象在其上绘制 8 位图像,然后将其保存为 Bmp。 这种方法不会出错,但是当我打开生成的图像时,BMP 标头具有各种时髦的值。 高度和宽度都很大,此外,压缩标志和其他一些标志还有有趣的(而且很大的)值。 不幸的是,我的特殊要求是将此文件传递给特定的打印机驱动程序,该驱动程序需要具有特定标头值的 24 位图像(我试图通过 GDI+ 来实现)

任何人都知道“上转换”索引的示例文件到无索引的 24 位文件? 如果没有示例,我应该从哪条路径开始编写自己的示例?

——凯文·格罗斯尼克劳斯 [电子邮件受保护]

Alright, I have an image coming through from an external application in an 8-bit indexed format. I need this image converted to a 24-bit format of the exact same size.

I've tried creating a new Bitmap of the same size and of type Format24bppRgb and then using a Graphics object to draw the 8-bit image over it before saving it as a Bmp. This approach doesn't error out but when I open the resulting image the BMP header has all kinds of funky values. The height and width are HUGE and, in addition, there are funny (and large) values for the compression flags and a few others. Unfortunately my particular requirements are to pass this file off to a specific printer driver that demands a 24-bit image with specific header values (which I'm trying to achieve through GDI+)

Anyone know of an example on "up-converting" an indexed file to a not-indexed 24-bit file? If not an example, which path should I start down to write my own?

-Kevin Grossnicklaus
[email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痕至 2024-07-22 01:13:19

问题可能是源图像和输出图像的垂直和水平分辨率之间的差异。 如果您加载分辨率为 72 DPI 的 8bpp 索引位图,然后创建一个新的 24bpp 位图(默认分辨率为 96 DPI...至少在我的系统上),然后使用 Graphics.DrawImage blit 到新的位图位图,您的图像将稍微放大并被裁剪。

话虽如此,我完全不知道如何正确创建输出位图和/或图形对象,以便在保存时正确缩放。 我怀疑这与使用常见比例(如英寸而不是像素)创建图像有关。

The problem is probably the difference between the Vertical- and HorizontalResolution of your source image and your output image. If you load a 8bpp indexed bitmap with a resolution of 72 DPI, and then create a new 24bpp bitmap (default resolution will be 96 DPI... at least it is on my system) and then use Graphics.DrawImage to blit to the new bitmap, your image will appear slightly zoomed in and cropped.

Having said that, I don't know off the top of my head how to properly create the output Bitmap and/or Graphics object to scale properly when saved. I suspect it will have something to do with creating the images using a common scale like inches instead of pixels.

云胡 2024-07-22 01:13:19

您创建的位图与输入的宽度和高度相同,但生成的 BMP 要大得多,这似乎很奇怪。 你能发布一些代码吗?

It seems odd that you're creating a Bitmap of the same width and height as your input, yet the generated BMP is much larger. Can you post some code?

魂ガ小子 2024-07-22 01:13:19

这是我的转换代码。 注意源图像和结果图像之间的分辨率匹配。

    private void ConvertTo24bppPNG(Stream imageDataAsStream, out byte[] data)
    {
        using ( Image img = Image.FromStream(imageDataAsStream) )
        {
            using ( Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb) )
            {
                // ensure resulting image has same resolution as source image
                // otherwise resulting image will appear scaled
                bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                using ( Graphics gfx = Graphics.FromImage(bmp) )
                {
                    gfx.DrawImage(img, 0, 0);
                }

                using ( MemoryStream ms = new MemoryStream() )
                {
                    bmp.Save(ms, ImageFormat.Png);
                    data = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(data, 0, (int) ms.Length);
                }
            }
        }
    }

This is my conversion code. Notice the matching of resolution between source image and resulting image.

    private void ConvertTo24bppPNG(Stream imageDataAsStream, out byte[] data)
    {
        using ( Image img = Image.FromStream(imageDataAsStream) )
        {
            using ( Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb) )
            {
                // ensure resulting image has same resolution as source image
                // otherwise resulting image will appear scaled
                bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                using ( Graphics gfx = Graphics.FromImage(bmp) )
                {
                    gfx.DrawImage(img, 0, 0);
                }

                using ( MemoryStream ms = new MemoryStream() )
                {
                    bmp.Save(ms, ImageFormat.Png);
                    data = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(data, 0, (int) ms.Length);
                }
            }
        }
    }
森罗 2024-07-22 01:13:19

我使用下面的代码将图像从 8bpp“上转换”为 24bpp。 使用十六进制编辑器检查生成的 24bpp 文件并与 8bpp 文件进行比较,显示两个文件的高度和宽度没有差异。 也就是说,8bpp 图像为 1600x1200,24bpp 图像具有相同的值。

    private static void ConvertTo24(string inputFileName, string outputFileName)
    {
        Bitmap bmpIn = (Bitmap)Bitmap.FromFile(inputFileName);

        Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
        using (Graphics g = Graphics.FromImage(converted))
        {
            // Prevent DPI conversion
            g.PageUnit = GraphicsUnit.Pixel
            // Draw the image
            g.DrawImageUnscaled(bmpIn, 0, 0);
        }
        converted.Save(outputFileName, ImageFormat.Bmp);
    }

标题中的其他所有内容看起来都很合理,并且图像在我的系统上显示相同。 您看到了什么“时髦价值观”?

I used the code below to "up-convert" an image from 8bpp to 24bpp. Inspecting the generated 24bpp file with a hex editor and comparing against the 8bpp file shows no difference in height and width in the two files. That is, the 8bpp image was 1600x1200, and the 24bpp image has the same values.

    private static void ConvertTo24(string inputFileName, string outputFileName)
    {
        Bitmap bmpIn = (Bitmap)Bitmap.FromFile(inputFileName);

        Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
        using (Graphics g = Graphics.FromImage(converted))
        {
            // Prevent DPI conversion
            g.PageUnit = GraphicsUnit.Pixel
            // Draw the image
            g.DrawImageUnscaled(bmpIn, 0, 0);
        }
        converted.Save(outputFileName, ImageFormat.Bmp);
    }

Everything else in the headers looks reasonable, and the images display identical on my system. What "funky values" are you seeing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文