Home » .Net Framework

POSPrinter - PrintMemoryBitmap


Parameters station

The printer station to be used. Can be either PrinterStation.Slip or PrinterStation.Receipt.


The bitmap to be printed.


Printed width of the bitmap.


Placement of the bitmap.


I faced problem in converting Image to Bitmap and print it through PrintMemoryBitmap method. 

                   Image img = Image.FromFile("C:\\Image.bmp");

                    byte[] byImage = ImageToByteArray(img);

                    Bitmap bitImage = BytesToBitmap(byImage);  

        public Bitmap BytesToBitmap(byte[] byteArray)


            using (MemoryStream ms = new MemoryStream(byteArray))


                Bitmap img = (Bitmap)Image.FromStream(ms);

                return img;




        public byte[] ImageToByteArray(Image img)


            using (MemoryStream ms = new MemoryStream())


                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

                return ms.ToArray();



I always hit this error.

ERROR:System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

           at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)

           at System.Drawing.Image.Save(Stream stream, ImageFormat format)


Did anyone use this method successfully, if yes, please kindly provide to correct method on convert Image to Bitmap Data.




12 Answers Found


Answer 1


Yes, I have used these methods before. I cannot see anything obviously wrong with your code, and when I tried it on my own PC it worked fine (with one modification) which leads me to believe there might be a problem with the image file you are using. Can you try a different image ?

As for the modification I had to make... don't use the 'using' statement on the stream in the BytesToBitmap method. The image object created relies on the stream it was loaded from, and if you dispose the stream then trying to use the image will create an error. So, change your method to be;

      public Bitmap BytesToBitmap(byte[] byteArray)


                return (Bitmap)Image.FromStream(new MemoryStream(byteArray));



Answer 2

I had made the changes accordingly and also using different image but it still return error

POSControlException ErrorCode(Failure) ExtendedErrorCode(0) occurred: Method PrintMemoryBitmap threw an exception.  The device cannot perform the requested procedure.
System.Runtime.InteropServices.COMException (0x80020006): Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.PointOfService.Legacy.LegacyProxy.InvokeMethod(String methodName, Object[]& parameters, Boolean[] byRef)

_printer.PrintMemoryBitmap(CurrentStation, bitImage,

200, -2);



Answer 3

Yeap, but that's a different error which probably has a different solution.

The error says the device cannot perform the requested procedure, and it says that based on an earlier internal exception which says a name is not known.


Have you got the correct name for your device, and is the SO properly configured etc. ? Do you have a sample or test app for your printer, provided by the manufacturer, and does that work ?

Have you opened, claimed and enabled the device before calling the PrintMemoryBitmap method ? Failign to do any one of those can cause the printer to be in a state where it isn't valid to print a bitmap.

Also, the bitmap may have to be in a specific format. See this previous forum post about how to convert an image to a 1bpp format which is required by most service objects.

Finally, make sure your printer returns true for the CapRecBitmap or CapSlpBitmap properties (depending on whether you are printing to a receipt or slip printer). Not all service objects/printers support printing bitmaps.

Answer 4

I'm using EPSON TM-T88IV for testing with Sample Application in C:\Program Files\Microsoft Point Of Service\SDK\Samples\Sample Application project.

I able to print out the image with PrintBitMap() but now i would like to implement PrintMemoryBitmap cause i would send the Image over the network and print it in my client PC instead sending Image Name.

CapRecBitmap propery is TRUE.

Can you provide me the sample image file you tested.


Answer 5


I used multiple images files, any bitmap image file seems to work ok. The Epson service objects are particularly good at converting the image to the right format for you, although you shouldn't rely on that if you want to be device independent. The link I mean to post for you before, re the image conversion, is here;


Can you post the code where you are actually printing the image ? Also, if you assign your converted image (the one returned from BytesToBitmap) to a picture box control does it display correctly ? Assuming it does it would seem the problem (probably) has to do with the printing commands rather than the image content.


Answer 6

Here is my (modfiied) code for printing an image, which works with the Epson printer and any image file that .Net can load (note the 'Image' object is called 'logo');

						int alignment = PosPrinter.PrinterBitmapCenter;

using (System.IO.MemoryStream ms = new MemoryStream()) { logo.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms)) { try { m_PosPrinter.PrintMemoryBitmap(PrinterStation.Receipt, bmp, PosPrinter.PrinterBitmapAsIs, alignment); } catch (PosControlException ex) { MessageBox.Show("Error printing logo (PrintMemoryBitmap); " + ex.Message); } } }

Answer 7

Same Error again....

i using



logo = Image.FromFile(dlg.FileName);

to load the image to image object.

is it the Image.bmp file problem?
Any sample image i can use in Windows XP?


Answer 8

If you want a sample image, create one with Windows Paint and save it as a bmp file (Windows Bitmap format). That should work fine. I doubt it is the file that is the problem though, since you have already succesfully loaded the image from the file and then converted it to a bitmap format in memory.

Your code to load the image looks ok.

Try creating your own bitmap with paint, and Can you post the code where you actually print the image ?

Answer 9

Also, if you copy my code in place of yours (updating any variables names etc. that are required) does it work ?

Answer 10

I just copy ur code and place into my sample application and also create a simple bmp file in paint and try. Still return same error.....help.....


Image logo = Image.FromFile(dlg.FileName);

alignment =



using (System.IO.MemoryStream ms = newMemoryStream())


logo.Save(ms, System.Drawing.Imaging.





using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms))






PrinterStation.Receipt, bmp, PosPrinter.PrinterBitmapAsIs, alignment);



catch (PosControlException ex)







Answer 11


I have temporarily uploaded a sample image that works here; http://www.yortsoft.com/blog/navimage.bmp

The following code (as the content of form1 in a new Windows Forms project) works perfectly on my pc. If this doesn't work for you, then I'm out of ideas... possibly there is a problem with your printer or service object. Note, in the code below you might need to change the logical name, or the way the printer is found before it will work on your PC.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.PointOfService;
using System.IO;

namespace WindowsFormsApplication1
	public partial class Form1 : Form
		public Form1()

		private void Form1_Load(object sender, EventArgs e)
			Image logo = Image.FromFile(@"c:\temp\navimage.bmp");

			int alignment =	PosPrinter.PrinterBitmapCenter;

			PosExplorer pe = new PosExplorer(this);

			PosPrinter printer = (PosPrinter)pe.CreateInstance(pe.GetDevice("PosPrinter", "ReceiptPrinter")); //ReceiptPrinter == my logical name for device on my PC.
			using (System.IO.MemoryStream ms = new MemoryStream())

				logo.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

				ms.Seek(0, SeekOrigin.Begin);
				using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms))

					printer.DeviceEnabled = true;


						printer.PrintMemoryBitmap(PrinterStation.Receipt, bmp, PosPrinter.PrinterBitmapAsIs, alignment);


					catch (PosControlException ex)







Answer 12

Thanks for your explanation! It is just the solution for my problem.


Hello, I have implemented a little console app that prints receipts on PosPrinter (Epson), and the layout of the receipts is all fine but I need to use variable font sizes to establish the correct branded *look and feel* to the receipts. I have been searching for a complete list of the escape sequences for the PosPrinter (UPOS compatible) but not been able to find a list. My printer documentation (Epson Application Development Guide) comes with a list of escape sequences it supports but no description of the what the actual sequences do.

Does any one have any input with regards to that?

The escape sequences must be reasonably printer agnostic ( I guess UPOS compliant). 

Thanks, Gus  


What I am trying to do is to get the print data send to IBM Suremark printer.

I was able to create a sample POSPrinter Service object and use it from .Net SDK sample app which prints data to a file. Now I want to either setup the IBM printer to use my service object so that I can get the data and then pass control to the IBM service Object named AIPOPOS.POSPRINTER.SO for the real processing or use my service object for entire printing process. I am not sure which way to go about this and what exactly to do about this. Sean L (your book) and Yort (your articles)  was really helpful for me to get up to this . I appreciate  any help/direction/input for this.


Thanks in advance.




Hey all.

I am sending text to a PosPrinter (Epson TM-H6000) and I can print logos and normal text perfectly. However, whenever I wish to print a pound sign "£" it shows it as a "u" with a line above it.

I have googled a bit, and then tried to do a string replace with (Char)156 and (Char)163, but none of these has worked either. One of them still shows the "u" and the other shows a "?", I assume as it doesnt know how to display it.

Any ideas?

Hi I'm a part of a team developing an Point Of Sale solution, we have come to the receipt printing and are looking for a neat way to print.
We had hoped that we could use Active Reports to design our receipts and then just tell the PosPrinter to print our ActiveReport object.
After some research all I can see are some Print methods on the PosPrinter object that just takes string inputs, I guess commands for the printer, when cutting paper and so on also are need to be inside these strings when using print.

So my question is can I use Active Reports with Pos?
If not are there any good example codes where I can se using posPrinter.Print() printing in a neat way, there tabs and printing from the right features isn't to hard to implement?
A small question to everyone, I am trying to get a barcode printed on the receipt from my TM-T88IV. The barcode string with only numbers are printing just frine, but I would like to include a couple of characters in front of the number and I am getting the below exception all the time.

[22:23:54] ERROR - [printReceipt 791] : Microsoft.PointOfService.PosControlException: "data"
 parameter is
 illegal. ---> jp.co.epson.upos.pntr.barcode.PrintBarCodeException: "data"
 parameter is
   at jp.co.epson.upos.pntr.barcode.Normal1DCodeCreator.CheckLength(Int32 iLength, Int32 iMin, Int32 iMax)
   at jp.co.epson.upos.pntr.barcode.Normal1DCodeCreator.CheckParameter_EAN13(Byte[] abyBarCodeData)
   at jp.co.epson.upos.pntr.barcode.Normal1DCodeCreator.CreateBarCodeCommand_EAN13(Byte[] abyBarCodeData)
   at jp.co.epson.upos.pntr.barcode.Normal1DCodeCreator.CreateBarCodeCommand(Byte[] abyBarCodeData)
   at jp.co.epson.upos.pntr.barcode.BarCodeCommandCreator.GetNormalCommand_1D(Base1DCode objBaseCode, Int32 iSymbologyType, Byte[] abyBarCodeData)
   at jp.co.epson.upos.pntr.barcode.BarCodeCommandCreator.GetNormalCommand(BaseCode objBaseCode, Int32 iSymbologyType, Byte[] abyBarCodeData)
   at jp.co.epson.upos.pntr.barcode.BarCodeCommandCreator.CreateBarCodeCommand(Int32 iSymbologyType, Byte[] abyBarCodeData, Boolean bWithRotateCommand)
   at jp.co.epson.upos.pntr.T88IVService.PrintBarCode(PrinterStation station, String data, BarCodeSymbology symbology, Int32 height, Int32 width, BarCodeAlignment alignment, BarCodeTextPosition textPosition)
   --- End of inner exception stack trace ---
   at jp.co.epson.upos.pntr.T88IVService.PrintBarCode(PrinterStation station, String data, BarCodeSymbology symbology, Int32 height, Int32 width, BarCodeAlignment alignment, BarCodeTextPosition textPosition)
   at Opos.PointOfService.BasicShimServiceObjects.PosPrinterShimBasic.PrintBarCode(PrinterStation station, String data, BarCodeSymbology symbology, Int32 height, Int32 width, Int32 alignment, BarCodeTextPosition textPosition)
ErrorCode: Illegal
ErrorCodeExtended: 1004

I would like to print a barcde of "rcpt10563" for example, so that when the barcode is read my application knows that its a reciept that has been scanned rather than a product.

Am I missing anything here? I have tried both BarCodeSymbology EanJan13 as well as Code128 but no luck.

I'm trying to print an Itf barcode and according to the PosPrinter.PrintBarCode() method the width is the width in units of measure.  I've tried many different values, but the barcode always prints the same width when printed with the NCR 7197 printer.  I don't have any other printers to test against.  What am I doing wrong?


Hi All. I have (Windows 2000/XP) clients connecting to a Windows 2003 Server through Remote Desktop that is hosting an application that will print some receipt. I am trying to get the POS Printer that is connected to the clients to be redirected properly in order to print. I have already checked out the link below but it seem to be only for Windows 2008 Server.


Can anyone explain how I could get the printer to be redirected or an alternative solution for Remote Desktop/Terminal Services.


Hello All.
  I have been writing a small application which uses a POS printer and everything is working OK. But now I have lost the use of the physical printer and want to use the device simulator in the POS .NET SDK to test my app with. But how do I use it ? How do I set up a virtual POS printer and where do I view the output after sending something to it ? Any help is appreciated.





I'm trying to write a posprinter service object by myself.  look through the documentation of microsoft point of service for .net.  but I can't understand how the application send datas to PosPrinter.  how the working principle of PrintNormal function? does it work use createfile and writefile?





This is a two parter... one part being far more important than the other.


part 1: (very important)


I'm allowing users to draw a 'layout' for a receipt with some pretty complex placement. It all works very nicely and when printing to a normal Windows Printer it comes out great.

Problem is the placement of everything on the stage is not by line, but just placed in any old spot the user desires. In windows printing this is fine because I just draw text to the print graphics object.


So over to POS I go. I started with OPOS and well... I had issues. I come to MS POS for .Net and similar issues arise (no surprise the two work rather similar).

Here I decide to get the nice layout I'll just draw bitmaps; create a Bitmap, create a Graphics from the bmp, draw to the graphics, write the Bitmap to the receipt printer with OPOS/MSPOS.

Well there in lies the hitch, it doesn't stream the bitmap to the printer, it sends the entire thing... which means if the bitmap is larger then the memory on the printer, then everything after it is thrown out (and only what of the bitmap that made it gets printed).


So I thought, hey I'll split it into clumps. Break the long bitmap (its height is long, it's a receipt) into segments. Let's say 10 'print lines' per segment (I measure the PosPrinter.RecLineHeight prop for this). And I print each segment one after the other. This 'works' to an extent... problem is that the Printer wants to put a linefeed after each bitmap sent to the printer. Causing a small gap between each image. This is no good!


Any ideas how I can deal with this? I tried reverse feeding one line after each print, but I don't think my printer even supports that (which means it's a no go). Please help.



Problem 2: (not as important)

Calling PosPrinter.PrintMemoryBitmap fails every time. I've been saving each bitmap down into a temp folder each time I need and printing from filename... no big deal for proof of concept and the sort right now. But it'd be really nice if I could figure out why the heck it doesn't work.

I save it as a BMP when I save to the local drive... and the Image is of type Bitmap in my code.


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure