C# Graphics error when creating a rectangle on top of a tif
What I have is a .tif file being loaded into a picture box. When I try to draw a rectangle on top of that tif it throws the following error:
A Graphics object cannot be created from an image that has an indexed pixel for开发者_StackOverflowmat.
here is the code i'm using, it throws the error at using (Graphics g = Graphics.FromImage(img)). img is the file being loaded into the picture box.
using (Graphics g = Graphics.FromImage(img))
{
int x1value = Convert.ToInt32(x1);
int y1value = Convert.ToInt32(y1);
int x3value = Convert.ToInt32(x3);
int y3value = Convert.ToInt32(y3);
SolidBrush blackBrush = new SolidBrush(Color.Black);
g.FillRectangle(blackBrush, x1value, y1value, x3value, y3value);
Image img = Image.FromFile(string2image);
Bitmap source = (Bitmap)img;
source = new Bitmap(320, 240, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
pictureBox1.Image = source;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
This method actually only draws a gaint black box. not causes a blackbox to be written over the original file.
You're trying to get a graphics context from an image. You need to get one from a Bitmap buffer i believe.
精彩评论