如果图像没有流,则设置图像调色板会引发GDI +通用错误异常 [英] Setting image palette throws GDI+ generic error exception if the image doesn't have a stream

查看:262
本文介绍了如果图像没有流,则设置图像调色板会引发GDI +通用错误异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GDI +将一些图像渲染到位图上,然后将位图渲染到面板上以用作编辑器.

I'm using GDI+ to render a few images onto a bitmap, then render the bitmap onto a panel to use as an editor.

在编辑器面板中选择图像时,该图像应突出显示为红色.我使用以下代码进行了工作

When an image in the editor panel is selected, it should highlight red. I had this working using the following code

If mCurrentindex = ind Then
    Dim redImage As Bitmap = item.Image.Clone()

    Dim pal As ColorPalette

    pal = redImage.Palette

    For i As Integer = 0 To pal.Entries.Length - 1
        If pal.Entries(i).Name = "ff000000" Then
            pal.Entries(i) = Color.Red
        End If
    Next

    redImage.Palette = pal

    g.DrawImage(redImage, 0, 0, (CType((item.Image.Width), Integer)), (CType((item.Image.Height), Integer)))

    Dim highlightPen As New Pen(Brushes.Red, 2)
    g.DrawRectangle(highlightPen, New Rectangle(0, 0, item.W - 1, item.H - 1))
Else
    g.DrawImage(item.Image, 0, 0, (CType((item.Image.Width), Integer)), (CType((item.Image.Height), Integer)))
End If

当我使用Image.FromFile加载图像时,这是可行的,它可以锁定文件,这是我不想要的.我更改了代码,使用蒸汽将图像加载到临时图像中,将其克隆到另一个图像中,然后处理该临时图像.但是,现在当我下线

This was working when I was loading the image using Image.FromFile, which locks the file up, which I don't want. I changed the code to load the image into a temporary image using a steam, clone this into the other image, then dispose of the temporary image. However, now when I hit the line

redImage.Palette = pal

我收到一般性的GDI +错误.任何一个受到打击的人都会知道,他们提供的基本信息不外乎出事了".我不确定为什么更改调色板会在原始图像中起作用,但在克隆的图像中不起作用.有人可以帮我吗?

I get a generic GDI+ error. Anyone whose hit one of these will know they give basically no more information than "something broke." I'm not sure why changing the palette would work in the original, but not the cloned image. Can anyone help me out here?

应注意,如果有所不同,则每个像素索引的图像为1位.

It should be noted that the images are 1 bit per pixel indexed if it makes a difference.

谢谢.

推荐答案

Bitmap(Image)和Bitmap.Clone()有什么区别

What's the difference between Bitmap(Image) and Bitmap.Clone()

这是 deep shallow 副本之间的区别. Bitmap.Clone()是浅表副本,不会复制位图的像素数据.它保持指向原始像素数据的指针.仅当您使用带Rectangle的重载之一时,它才真正有用.

It is the difference between a deep and a shallow copy. Bitmap.Clone() is a shallow copy that doesn't copy the pixel data of the bitmap. It keeps a pointer to the original pixel data. It is really only useful when you use one of the overloads that takes a Rectangle.

因此,Bitmap.Clone()将锁定像素数据的基础源.就像您从中加载图像的文件一样.如果您使用MemoryStream锁定文件,则为流.哪些确实要求您保持MemoryStream处于活动状态.关闭或处置它会使您的程序崩溃.稍后,当需要像素数据时.通常在绘制时.

Accordingly, Bitmap.Clone() will leave a lock on the underlying source of the pixel data. Like the file from which you loaded the image. Or the stream if you used MemoryStream to the lock on the file. Which does require you to keep the MemoryStream alive. Closing or disposing it will crash your program. Later, when the pixel data is needed. Typically when it is painted.

通过创建不锁定文件的深层副本来避免所有这些情况:

Avoid all this by creating a deep copy that doesn't lock the file:

    public static Bitmap LoadBitmapWithoutLock(string path) {
        using (var temp = Image.FromFile(path)) {
            return new Bitmap(temp);
        }
    }

这篇关于如果图像没有流,则设置图像调色板会引发GDI +通用错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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