bit depth change

Posted on 2012-08-29
Last Modified: 2012-08-29
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

Question by:mahmuod
    LVL 13

    Expert Comment

    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) ".

    Author Comment

    thank you  frankhelk
    the Bit depth=32 not change
    LVL 35

    Accepted Solution

    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:

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    I recently asked a question ( about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now