Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3225
  • Last Modified:

bit depth change

i want create image in picturebox and then save to file but when save file the
bit depth=32
i am i want bit depth=1
how to change bit depth from 32 to 1bit depth
  Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format16bppArgb1555);

       Graphics g;

       bmp = new Bitmap(bmp, pictureBox1.Width, pictureBox1.Height);
       g = Graphics.FromImage(bmp);
       using (Font myFont = new Font("Arial", 30))

           g.DrawString(textBox2.Text, myFont, Brushes.Black, new Point(2, 2));
       pictureBox1.Image = bmp;

       pictureBox1.Image.Save(@"C:\data\mycode.Bmp", ImageFormat.Bmp);

Open in new window

1 Solution
You'll just need to use another constructor. Try
bmp = new Bitmap(bmp, pictureBox1.Width, pictureBox1.Height, Format1bppIndexed);

Open in new window

Maybe you'll have so set which colors to be used for the states of the bit.

For reference use VS help, Bitmap class, constructor "Bitmap(Int32, Int32, PixelFormat) ".
mahmuodAuthor Commented:
thank you  frankhelk
the Bit depth=32 not change
Robert SchuttSoftware EngineerCommented:
The problem is that, after using a constructor with any indexed color format (such as Format1bppIndexed) you can't paint the text in it. An error is given when you call Graphics.FromImage: "A Graphics object cannot be created from an image that has an indexed pixel format."

There's a number of solutions that can be found, not sure which one would be best but seeing as I hate to refer to another forum, this one didn't seem half bad: http://www.news2news.com/vfp/?example=493&ver=vcs&PHPSESSID=35494364de9987dfd2a9f3fe18f17565

Converted to your situation I come to this:
private const int LR_LOADFROMFILE = 0x0010;
private const int LR_MONOCHROME = 0x0001;

static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);

static extern bool DeleteObject(IntPtr hObject);

private void button2_Click(object sender, EventArgs e) {
    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    Graphics g = Graphics.FromImage(bmp);
    using (Font myFont = new Font("Arial", 30)) {
        g.DrawString("test", myFont, Brushes.Black, new Point(2, 2));
    pictureBox1.Image = bmp;

static void SaveMonochromeBitmap(Image im) {
    string tempFile = Path.GetTempFileName();
    tempFile = Path.ChangeExtension(tempFile, "bmp");
    try {
        im.Save(tempFile, ImageFormat.Bmp);
        IntPtr hBitmap = LoadImage(IntPtr.Zero, tempFile, 0, 0, 0, (LR_LOADFROMFILE | LR_MONOCHROME));
        Image img = Image.FromHbitmap(hBitmap);
        img.Save(@"C:\data\mycode.Bmp", ImageFormat.Bmp);
    catch (Exception e) {

Open in new window


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now