在c#中导出透明图像?

Ale*_*lex 1 c# transparency

我已经在c#中编辑了一个位图,并且对于每个像素,如果条件为真,我将其更改为某种颜色,否则我将颜色设置为Color.Transparent(操作使用getPixel/setPixel完成).我以.png格式导出图像,但图像不透明.任何想法为什么或如何做?

此致,Alexandru Badescu

这里是代码: - 这里我加载图像并转换为PixelFormat.Format24bppRgb如果png

m_Bitmap =(Bitmap)Bitmap.FromFile(openFileDialog.FileName,false);

           if(openFileDialog.FilterIndex==3) //3 is png
            m_Bitmap=ConvertTo24(m_Bitmap);
Run Code Online (Sandbox Code Playgroud)

- 这用于在矩阵中的某个位置之后改变像素

for(int i = startX; i <endX; i ++)

                for (int j = startY; j < endY; j++)
                {
                    if (indexMatrix[i][j] == matrixFillNumber)
                        m_Bitmap.SetPixel(j, i, selectedColor);
                    else
                        m_Bitmap.SetPixel(j, i, Color.Transparent);

                }
Run Code Online (Sandbox Code Playgroud)

hon*_*bis 5

它因为pixelformat.以下是您的示例代码:

        Bitmap inp = new Bitmap("path of the image to edit");
        Bitmap outImg = new Bitmap(inp.Width, inp.Height, PixelFormat.Format32bppArgb);
        outImg.SetResolution(inp.HorizontalResolution, inp.VerticalResolution);
        Graphics g = Graphics.FromImage(outImg);
        g.DrawImage(inp, Point.Empty);
        g.Dispose();
        inp.Dispose();

        ////
        // Check your condition and set pixel here (outImg.GetPixel, outImg.SetPixel)
        ////

        outImg.Save("out file path", ImageFormat.Png);
        outImg.Dispose();
Run Code Online (Sandbox Code Playgroud)

这是需要对当前代码进行最少更改的代码.但我建议你查看LockBits方法以获得更好的性能:http: //msdn.microsoft.com/en-us/library/5ey6h79d.aspx