Solved

Graphics/Bitmap Object: Save graphic as 1bpp

Posted on 2004-08-26
8
984 Views
Last Modified: 2013-12-03
My application creates a dynamic graphic (some numbers superimposed on an existing bitmap) and saves the merged result to a bitmap file.  I developed this using a laser printer.  However, my customer will be using it on a thermal receipt printer.  It, unfortunately, will only acept a 1 bit bitmap.

My delima:
My base graphic, is saved as a grayscale file.  I do my text/graphic merge thingy and save it back as a file, but the receipt printer won't print it because it is not 1 bit black and white graphic.

So  I changed the base graphic to a 1 bit bitmap.  When I run my app, an exception get thrown, "A Graphics object cannot be created from an image that has an indexed pixel format..." in other words something les than 16bpp.

Here's the offending code:
               'Build dynamic graphic here
                Const X = 160, Y = 160

                'Graphics Class
                Dim objBitmap = Bitmap.FromFile(regKey.GetValue("backgroundBloominBuck"))
                Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)

                'Text
                Dim moneyFont As New Font("Verdana", 36, FontStyle.Bold)
                Dim drawBrush As New SolidBrush(Color.Black)
                Dim drawFormat As New StringFormat

                drawFormat.FormatFlags = StringFormatFlags.NoWrap
                objGraphics.DrawString(strMoney, moneyFont, drawBrush, 66, 66, drawFormat)
                objBitmap.Save(regKey.GetValue("dynamicBloominBuck"), ImageFormat.Bmp)

If I could leave my orginal graphic as a grayscale, but somehow programmatically convert the resulting output file to a 1 bpp bitmap, then I would be happy.  I would also be just as happy, if I could use Graphics.FromImage using a 1 bpp file. I wish I had run into this problem sooner, because this has to go live Saturday morning.

Someone please have pity on me and enlighten me with thine grand wisdom from I am but a worm.
0
Comment
Question by:carlajasminelewis
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:dimitry_dimitry
ID: 11910446
Use pixelfomat property.
Bitmap.PixelFormat=PixelFormat.Format1bppIndexed
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11911689
>>Use pixelfomat property.
>>Bitmap.PixelFormat=PixelFormat.Format1bppIndexed

That is a read-only property.
0
 
LVL 5

Expert Comment

by:rsriprac
ID: 11918764
USe the following function:

Bitmap.Clone Method (RectangleF, PixelFormat)

and clone into a new object and then use that object.

-Ram
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11919157
>USe the following function:
>Bitmap.Clone Method (RectangleF, PixelFormat)
>and clone into a new object and then use that object.

Had already tried that too.  As long as the PixelFormat is above 8bpp it will work, but for 8bpp, 4bpp, and 1bpp it throws an "Out of memory" exception.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
rsriprac earned 500 total points
ID: 11919199
0
 
LVL 1

Author Comment

by:carlajasminelewis
ID: 11919230
I know I posted this in a general forum, and I'm not that familiar with C#.  I am able determine what the guy is doing here, and I think it may be the shortest, quickest solution that has I have come across.  I'm sure I can make it work just fine.
0
 
LVL 5

Expert Comment

by:rsriprac
ID: 11919308
I'm sure you know this, but for .NET you could cross-compile languages.  Then again if C# isn't you thing, then I guess it worth re-doing it in VB. At the end tho, VB should have most of its equvelents of C#.  (=

-Ram
0
 
LVL 5

Expert Comment

by:rsriprac
ID: 11919312
Good luck with that too.

-Ram
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

21 Experts available now in Live!

Get 1:1 Help Now