Home » .Net FrameworkRSS

POSPrinter - PrintMemoryBitmap

Hi,

Parameters station

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

data

The bitmap to be printed.

width

Printed width of the bitmap.

alignment

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.

Thanks.

 

 

12 Answers Found

 

Answer 1

Hi,

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);

HELP!!

 

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.

So,

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.

Thanks..
 

Answer 5

Hi,

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;

http://social.msdn.microsoft.com/Forums/en-US/posfordotnet/thread/318ef219-c6f6-42a8-9756-6bbda2cb82e0

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

Hi,
Same Error again....

i using

Image

 

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 =

PosPrinter.PrinterBitmapCenter;

 

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

{

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

{

_printer.PrintMemoryBitmap(

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

}

 

catch (PosControlException ex)

{

ShowException(ex);

}

}

}

 

Answer 11

Hi,

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()
		{
			InitializeComponent();
		}

		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.Open();
					printer.Claim(1000);
					printer.DeviceEnabled = true;

					try
					{

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

					}

					catch (PosControlException ex)
					{

						MessageBox.Show(ex.ToString());

					}

					printer.Release();
					printer.Close();
				}

			}

		}
	}
}


 

Answer 12


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

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter