C# redaction drawing on Image
<p>Whats going on is i need to draw a black rectangle over the image.  I have to load a tif and then show a blackbox over it.  I was helped with some code but i continously got the error: A Graphics object cannot be created from an image that has an indexed pixel format.</p>

<p>So i had to read it in to bit format, but when i display the box it resizes the box wierd. And completly displays the the picture box in all black nothing of the original image.  if someone could help me where i'm going wrong that would be awesome.</p>

<pre><code>Bitmap original = (Bitmap)System.Drawing.Image.FromFile(coveted, true);
                                Bitmap newImage = new Bitmap(original.Width, original.Height);
                                pictureBox1.Image = newImage;
                                using (Graphics g = Graphics.FromImage(pictureBox1.Image))
                                {
                                    using (SolidBrush brush = new SolidBrush(Color.Black))
                                    {
                                        g.FillRectangle(brush, new Rectangle(x1value, y1value, x3value, y3value));
                                    }
                                }
</code></pre>

<p>I'm not sure how I can make this clearer.  Whats happening is I have a tif in a unsupported format.  So I have to change it to a Bitmap so I can actually draw a rectangle on it.  Then I need to display this redacted image (the original with the redaction) in a picturebox.  Whats going on with the code above, is once it's completed, all it displays is a blackbox with no original image.</p>

<p>I believe i ran something about using a Bitmap from stream and then closing the stream.  Anybody familiar with this?</p>

<p>Thanks to all the help from STO members!!  heres the correct code for redacting images if you encounter the error "A Graphics object cannot be created from an image that has an indexed pixel format.".
if you're given the redacted starting points (obviously you have to make the Regex work to your situation):</p>

<pre><code>//Regex for pulling points from a file
string x1 = x1 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)").Groups[2].Value;
                        string y1 = y1 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)\r\n(\d+)").Groups[3].Value;
                        string x2 = x2 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)").Groups[4].Value;
                        string y2 = y2 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)").Groups[5].Value;
                        string x3 = x3 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)").Groups[6].Value;
                        string y3 = y3 = Regex.Match(l, @"\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)\r\n(\d+)").Groups[7].Value;
                        {
//convert string to int for redacted points
                            int x1value = Convert.ToInt32(x1);
                            int y1value = Convert.ToInt32(y1);
                            int x3value = Convert.ToInt32(x3);
                            int y3value = Convert.ToInt32(y3);
                            {
//BEGIN Workaround for indexed pixels
                                Bitmap original = (Bitmap)System.Drawing.Image.FromFile(YOURFILE, true);
                                Bitmap newImage = new Bitmap(original);
                                pictureBox1.Image = newImage;  //END Workaround for indexed pixels
                                using (Graphics g = Graphics.FromImage(pictureBox1.Image))  //start redaction
                                {
                                    using (SolidBrush brush = new SolidBrush(Color.Black))
                                    {
                                        g.DrawImageUnscaled(newImage, 0,0);
                                        g.FillRectangle(brush, new Rectangle(x1value, y1value, x3value, y3value));
                                    }
                                }  //End Redaction
                                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //Resized to fit into a static picturebox
                            }
                        }
</code></pre>
