Graphics on indexed image

前端 未结 3 2003
礼貌的吻别
礼貌的吻别 2020-12-09 02:11

I am getting error:

\"A Graphics object cannot be created from an image that has an indexed pixel format.\"

in function:

3条回答
  •  清歌不尽
    2020-12-09 02:46

    Refering to this, it can be solved by creating a blank bitmap with the same dimensions and the correct PixelFormat and the draw on that bitmap.

    // The original bitmap with the wrong pixel format. 
    // You can check the pixel format with originalBmp.PixelFormat
    Bitmap originalBmp = new (Bitmap)Image.FromFile("YourFileName.gif");
    
    // Create a blank bitmap with the same dimensions
    Bitmap tempBitmap = new Bitmap(originalBmp.Width, originalBmp.Height);
    
    // From this bitmap, the graphics can be obtained, because it has the right PixelFormat
    using(Graphics g = Graphics.FromImage(tempBitmap))
    {
        // Draw the original bitmap onto the graphics of the new bitmap
        g.DrawImage(originalBmp, 0, 0);
        // Use g to do whatever you like
        g.DrawLine(...);
    }
    
    // Use tempBitmap as you would have used originalBmp
    return tempBitmap;
    

提交回复
热议问题