Solved

Graphics/Bitmap Object: Save graphic as 1bpp

Posted on 2004-08-26
8
977 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

19 Experts available now in Live!

Get 1:1 Help Now