Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:13:11+00:00 2026-05-13T13:13:11+00:00

Whats going on is i need to draw a black rectangle over the image.

  • 0

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.

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.

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));
                                    }
                                }

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.

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

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):

//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
                            }
                        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:13:12+00:00Added an answer on May 13, 2026 at 1:13 pm

    Instead of

    Bitmap newImage = new Bitmap(original.Width, original.Height);
    

    you want

    Bitmap newImage = new Bitmap(original);
    

    This will make your newImage start with the contents of original.
    The difference will be that you will end up with newImage.PixelFormat == PixelFormat.Format32bppArgb, while I’m assuming original.PixelFormat == PixelFormat.Format1bppIndexed.
    With PixelFormat.Format32bppArgb, you can create a Graphics object; you cannot with PixelFormat.Format1bppIndexed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, my need is to have a background Image in my java app,
In terms of strings I understand the need to have strings am going to
Can anyone explain whats going on 'under the hood' with textRanges in IE/javascript? What
Not sure what's going on here. I have a DateTime object, and when I
I am just trying to understand what is going on here. I have a
I have two classes, let's call them parent and child, and both need to
I'm trying to draw on some canvas elements and, of course, I need it
I am currently developing an app that's going to show a Facebook page's events
What's going on here? printf.sh: #! /bin/sh NAME=George W. Bush printf Hello, %s\n $NAME
What's going on?! Subtraction works fine until I get to 0.1 - 0.1. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.