I am very new in ESC POS command. I have uploaded images to printer memory. I am able to print required image with utility comes with printer. I want to print image from my application. I don't know really how to print it using C#. I am using below code to print but it is not printing anything. Can you please guide me how to print image using C#.
public void PrintImage()
{
const string FS = "\u001C";
string command = FS + "p11";
WriteLine(command);
}
public void WriteLine(string text)
{
WriteToBuffer(text);
_writeByte(10);
System.Threading.Thread.Sleep(WriteLineSleepTimeMs);
}
private void _writeByte(byte valueToWrite)
{
byte[] tempArray = {valueToWrite};
_serialPort.Write(tempArray,0,1);
}
Basically, ESC/POS prints with byte[] instead of string(Unicode).
Actually, WriteToBuffer() seems to send the character string and command of the print request, but if there is no source for this part, an accurate answer cannot be made.
The command you are going to use is this one, which prints the NV image defined by the "FS q" command.
This is a deprecated(non-recommended?/obsolete?) mode in the included utility and should not be used.
FS p [obsolete command] : Print NV bit image
FS q [obsolete command] : Define NV bit image
However, if you really want to use the "FS p" command, the byte data sent to the printer will be:
byte[] NVimageCmd = { 0x1c, 0x70, 0x01, 0x31 };
The NV image number that should be specified is the numeric value 1 (0x01), not the character '1'.
Another command is currently recommended for use, as described below in the command description above.
[Recommended Functions]
This function is supported only by some printer models and may not be supported by future models. It is recommended that NV graphics function (GS ( L / GS 8 L: GS ( L <Function 51> and GS ( L <Function 64> – GS ( L <Function 69>) be used because they offer the following additional features:
Use the following command to print the NV image registered with the supplied utility.
kc1 and kc2 are the key codes specified when registering.
GS ( L <Function 69> : Print the specified NV graphics data.
By the way, if the printer and utility are in Advanced Printer Driver mode, send the control font according to the explanation of that mode.
Related
I'm writing out a file in C++ as:
void TestExportWriteOut()
{
const auto filePath = R"(C:\TestOut.bin)";
ofstream outFile(filePath, ios::out | ios::binary);
outFile<< 1;
outFile.flush();
outFile.close();
}
and trying to read it in C# as:
[TestMethod]
public void LoadTestOut()
{
const string filePath = "C:\\TestOut.bin";
using (var fileStream = File.OpenRead(filePath))
using (var reader = new BinaryReader(fileStream))
{
var intOne = reader.ReadInt32(); //!!Throws Exception
Assert.AreEqual(intOne, 1);
}
}
However, right at the line where I try to read the integer value 1, I get an exception saying:
System.IO.EndOfStreamException: 'Unable to read beyond the end of the stream.'
What's the correct way of reading files in C# that are created using ofstream in C++?
I can see the file created by C++ on the file system, and while all I wrote to it is an int, its size is 1KB.
The ios::binary flags does not do what you think(*). It doesn't influence the behavior of operator<<(int) which still put a text representation of its argument. On the C++ side, use outFile.write() to output the binary representation (in conjunction with the ios::binary flag).
(*) It does open the file as a binary file for OS which have a notion of binary file different of text file (neither Windows nor Unix are in that class) and it prevent the transformation of \n in the normal way of representing end of line for the OS (Windows use a pair of characters for that, that's why you need the ios::binary flag)
I am trying to manipulate a PDF, it functions as a template. What I am trying is replacing 'placeholders' in the PDF template with my data. So someone makes a PDF template in Scribus for example, and adds an empty image with the name "company_logo". My application sees an image placeholder with the name "company_logo" and it adds the company logo there.
I can browse AcroFields with iTextSharp library and set text in a text field (for example) but AcroFields doesn't list the image placeholder. I've got the feeling that AcroFields is not what I am looking for.
So how can I get a list (or tree) of all objects from the PDF and read their properties (like position, size, contents, etc).
P.S. I do not necessarily need to use iTextSharp, any other PDF lib will do as well. Preferably free.
A little pseudo code to make myself more clear
var object = Pdf.GetObjectById("company_logo");
object.SetValue(myImage);
object.SetPosition(x, y);
From your pseudo-code example, we understand that you want to replace the stream of an object that contains an image. There are several examples on how to do this.
For instance, in the SpecialID example, we create a PDF where we mark a specific image with a special ID. In the ResizeImage example, we track that image based on that special ID and we replace the stream:
object = reader.getPdfObject(i);
if (object == null || !object.isStream())
continue;
stream = (PRStream)object;
if (value.equals(stream.get(key))) {
PdfImageObject image = new PdfImageObject(stream);
BufferedImage bi = image.getBufferedImage();
if (bi == null) continue;
int width = (int)(bi.getWidth() * FACTOR);
int height = (int)(bi.getHeight() * FACTOR);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR);
Graphics2D g = img.createGraphics();
g.drawRenderedImage(bi, at);
ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
ImageIO.write(img, "JPG", imgBytes);
stream.clear();
stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);
stream.put(PdfName.TYPE, PdfName.XOBJECT);
stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
stream.put(key, value);
stream.put(PdfName.FILTER, PdfName.DCTDECODE);
stream.put(PdfName.WIDTH, new PdfNumber(width));
stream.put(PdfName.HEIGHT, new PdfNumber(height));
stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
}
You will find another example in the book The Best iText Questions on StackOverflow where I answered the following question: PDF Convert to Black And White PNGs
I wrote the ReplaceImage example to show how to replace the image:
public static void replaceStream(PRStream orig, PdfStream stream) throws IOException {
orig.clear();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
stream.writeContent(baos);
orig.setData(baos.toByteArray(), false);
for (PdfName name : stream.getKeys()) {
orig.put(name, stream.get(name));
}
}
As you can see, it isn't as trivial as saying:
var object = Pdf.GetObjectById("company_logo");
object.SetValue(myImage);
As I explained in my comment, this doesn't make sense:
object.SetPosition(x, y);
The objects we're manipulating are streams that are used as Image XObjects. The advantage of having Image XObjects is that they can be reused. For instance: if you have the same logo on every page, then you want to store the bytes of that image only once and reuse the same logo multiple times. This means that the object with the image bytes doesn't know anything about its position. The position is determined in the content stream. It depends on the CTM.
Did you have a look at the scribus scripting capabilities?
Since you create a tamplate in scribus You could also write a short python script which replaces your placeholders with your final data and exports the final PDF.
Since scribus 1.5 it is also possible to call the python scripts from the commandline.
I'm trying to print my language characters to a POS printer. The Printer prints well but the result's so bad. This is what I tried:
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(AsciiControlChars.Escape);
bw.Write('#');
//ESCCMD.RenderBitmap(bw, logo);
bw.Write("Đây là Tiếng Việt");
bw.Write(AsciiControlChars.Escape);
bw.Write('d');
bw.Write((byte)3);
// Feed 3 vertical motion units and cut the paper with a 1 point uncut
bw.Write(AsciiControlChars.GroupSeparator);
bw.Write(AsciiControlChars.V);
bw.Write((byte)66);
bw.Write((byte)3);
bw.Flush();
RawPrinterHelper.SendToSerialPort(ms.ToArray(), txtPortTest.Text, Convert.ToInt32(cbbBaudRate.SelectedValue));
}
So how can I print my language characters using ESC/POS command?
Thanks so much!
Before printing international characters you need to check if your specific model supports the corresponding codepage and then set it with the ESC t command. The list of supported code pages for EPSON printers and the command syntax info is available here: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=32 (registration required)
For example, in order to print Greek (ISO-8859-7) text, you need to do something like this:
private void PrintGreekIsoText(BinaryWriter bw, string text)
{
// ESC t 15
bw.Write("\x1bt\x15");
// Convert the text to the appropriate encoding
var isoEncoding = Encoding.GetEncoding(28597);
var bytes = Encoding.Unicode.GetBytes(text);
byte[] output = Encoding.Convert(Encoding.Unicode, isoEncoding, bytes);
bw.Write(output);
}
I've got to interface my C++ project with a C# project and send an image to it across a named pipe. OpenCV stores matrix data in a contiguous chunk, starting at uchar* Mat::data. However, to send data across a named pipe, it must be a char*, so I just do a cast, not sure what else I'm supposed to do. It shouldn't matter, it's just data, not characters.
size_t s = frame2.elemSize() * frame2.rows * frame2.cols;
sendChars(hPipe, (char*)frame2.data, s);
On the C# side I read in the block of data to a char[] buffer. I then create a new Bitmap with the appropriate width, height, etc. and set the IntPtr to the beginning of the char[] buffer.
//in the C# forms private scope:
char[] buffer = new char[921600];
and then in a StartServer() function:
pipeServer = new NamedPipeServerStream("SamplePipe", PipeDirection.InOut);
//blah blah blah
using (StreamReader sr = new StreamReader(pipeServer))
{
sr.ReadBlock(buffer, 0, buffer.Length);
unsafe
{
fixed (char* ptr = buffer)
{
using (Bitmap image = new Bitmap(640, 480, 640*3, PixelFormat.Format24bppRgb, new IntPtr(ptr)))
{
pictureBox1.Image = image;
image.Save("test.png");
}
}
}
}
The result of this is a big red X on the C# form and the saved image looks garbled... but not just random noise. So the data's coming through and getting messed up either on the C++ end before transmission or the C# end on interpretation. I have tried to work around this by using the Bitmap constructor that reads a stream and sending an encoded image instead of the raw pixels, but that fails harder than the above method (just gives an error, no output).
How do I transfer uchar* array with pixel data from C++ to C# and then reconstruct it inside a Bitmap so that I can display it on the form?
The use of a StreamReader here seems like the wrong choice if what you're sending is actually binary data. The StreamReader is used for reading text, and will decode the data to characters with some encoding which in your case would be auto-detected (and definitely wrong).
You want to use the BinaryReader for reading binary data, or read directly from the NamedPipeServerStream using the Read() method to get a byte[] array. A char in C# is used for storing characters, and is unicode, not a single byte.
If you use stream the easiest way would be to use the SetPixel method of Bitmap to construct the image.
I am working on write a program that reads and writes from the barcode
my problem when writing (send command to barcode)
I read in pdf manufacturer barcode that the command of capturing the image is IMGSNP so i pass it to write function as follows serialPortObj.write ("IMGSNP")
But Why does not have a barcode to respond to the command ? and did not capture the image :(
Is this wrong way
(I have in some cases may need to take image Not for barcode it self but image for passport or product etc.. Which doesn't contains a barcode )
Barcode manufacturer is HandHeld (4800p)
Thanks for any help
here is my code
private SerialPort Com ;
private delegate void DataReadDel(string Text);
private void Form1_Load(object sender, EventArgs e)
{
Com = new SerialPort("COM4");
Com.Open();
Com.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void port_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
Com.Encoding = Encoding.Default;
this.BeginInvoke(new DataReadDel(DataReceived), new object[] {Com.ReadExisting() });
}
private void DataReceived(string dR)
{
textBox1.Text = dR;
}
private void button1_Click(object sender, EventArgs e)
{
if (! Com.IsOpen )
{
Com.Open();
}
Com.Write("IMGSNP1B1L");
Com.Write("IMGSHP");
string imgbytes = Com.ReadExisting();// return ""
}
You need to send the Serial command header along with the IMGSNP command to have the scanner capture and send the image. The header is three ASCII characters: SYN M CR (ASCII 22,77,13).
Are you sure that the barcode reader is not capturing the image? According to the documentation:
An image is taken when the Image Snap (IMGSNP) command is processed. The last image is always stored in memory. You may “ship” the image by using the IMGSHP command.
So you may be taking the image using IMGSNP, but all that's happening is that its storing the image in memory, and not sending it back to you as a response. Try then issuing the IMGSHP command, and see if there's any data to be read from your serial port.
You need to send the SYN M CR first before the device considers taking your request in.
The following information is obtained with my own barcode reader while trying to do something quite similar.
When using IMGSHP, the device will reply with a SYN (0x16) followed by 0xfe [4 bytes of data length, little endian] 0x0d [some data] 0x1d [image data]
As well as the command itself, do you have to provide any termination characters?
A common method is to wrap the command packet in STX and ETX characters so the barcode reader will know when a full command has been received, or possibly just terminate with a carriage return (CR) and linefeed characters (LF). You need to check the specs.
Also, since you are sending a string, encoding may be important, and I would expect the barcode reader needs ASCII characters sent to it, but again, you should check the unit to be sure.
I suspect the easiset way to send a command is to send it as an array of bytes, which makes it easier to use the STX, ETX, CR or LF characters if necessary, as these are non-printable characters and have the following values:
STX = 0x02, ETX = 0x03, CR = 0x0D, LF = 0x0A