Two GC420t printers different ZPL Outcome (ZPL, C#) - c#
i have 2 Zebra GC420t printers and a wpf application creating and sending ZPL code to selected printers.
The printed labels are too way different. See the image below.
The outcomes and printer configurations
Pinted ZPL code is: (label is 10x8.1 cm)
^XA
^PON
^CI27
^MMT
^PW800
^LL648
^FX Pictogram0
^FO632,104
^GFA,1300,1300,13,,W0G1GCM0W0G3G4M0,::::::::::::::::::::::::S0G7GCQ0P0HFG1HFGEP0K0G4H0G3IFGBJFG8G0G3L0K0G7G0G7JFGBG8G1IFGCG7L0K0G3JFG0G1GFG8H0IFGEL0K0G3HFGCH0G1GFG8I0G3GFGEL0K0G3GFJ0G7GBHFG8I0GEL0K0G3G8G0G3IFG9KFG0GEL0K0G3LFG0G7KFGEL0K0G3KFG8H0G7JFGEL0K0G1IFGEK0G1IFGCL0K0G1HFG8N0G7GFGCL0K0G1GEQ0G1GEG0G3G8I0K0G3GCO0G6G0G1GEH7G8I0K0G3GCO0H7G1GEH7J0K0G3GCO0G7GFG1GEG7G6J0K0G3GCO0G3GEG1GEGFG6J0K0G3GCO0G3GFG8GCGFGEJ0K0G1G8O0G3GFG9GCGDGEJ0I0G2G0G3G8G1G8M0G3GFG9GEGFG8J0I0G7G0G3G9GBGCN0GFG9HFG8J0I0G7GBG3G9GFG8N0GFGDHFG8J0I0G3GFG7GDGFO0GFGDGFGBK0I0G3GFG7GDGFO0G7G9GEG2K0I0G3GEG7HFO0G3G1GCL0I0G3JFN0G3GFG8G3L0I0G1GFGBGFGEN0G3IFGCK0J0GFG8G3GCN0KFG8J0J0GFG8GFGCM0G7LFG8I0J0G7GCGFG8M0G7JFG3GFG8I0G3JFGCG7JFI0G1HFGEG7H0G1G8I0G3JFG8G1JFI0G3HFGEG7G8L0G3PFI0G7HFGEG3GFL0G3PFI0G7HFGEG1GFL0G3PFI0G3HFGEG0GFL0G3PFI0G1HFGEN0G3PFJ0G7HFG8H0G3G8I0G3PFJ0G7HFGCG0G1GFG8I0G3PFJ0G1HFG8G1HFG8I0V0KFGCJ0V0G7IFGEK0X0G7G8L0,:::::::::::::::::::::::::::::
^FS
^FX Pictogram1
^FO632,280
^GFA,1300,1300,13,,:::::::::::::::Q0GFGES0P0G7HFGCR0P0IFGER0O0G3JFG8Q0O0G7JFGCQ0::O0KFGEQ0:O0G7JFGCQ0:::::O0G3JFG8Q0::::::O0G1JFR0::::::P0IFGER0:::::P0G7HFGCR0::::::P0G3HFG8R0::P0G1HFS0Q0GFGES0Q0G3G8S0,::Q0G7GCS0P0G1HFS0P0G3HFG8R0P0G7HFGCR0P0IFGER0:O0G1JFR0::::::P0IFGER0P0G7HFGCR0:P0G1HFS0Q0GFGES0,::::::::::::::
^FS
^FX Pictogram2
^FO632,456
^FS
^FX ItemNumber
^CF0,30
^FO25,27,0
^FB608,1,,L,
^FH
^FD
10001A
^FS
^FX ItemName
^CF0,50
^FO25,55,0
^FB608,1,,L,
^FH
^FD
_52_55_43_4F_47_45_4E_20_44_46_4C_20_33_30_30
^FS
^FX BatchNumber
^CF0,50
^FO25,105,0
^FB608,1,,L,
^FH
^FD
17-0001
^FS
^FX Amount
^CF0,30
^FO25,150,0
^FB608,1,,L,
^FH
^FD
120 Kg
^FS
^FX LabelDate
^CF0,30
^FO25,185,0
^FB608,1,,L,
^FH
^FD
29.05.2017
^FS
^FX Barcode
^FO25,230,0
^BY3,2,75
^BCN
^FD
869316388539
^FS
^FX Warnings
^CF0,20
^FO25,350,0
^FB560,30,,,L,
^FH
^FD
_48_33_30_32_3A_20_59_75_74_75_6C_6D_61_73_69_20_68_61_6C_69_6E_64_65_20_7A_61_72_61_72_6C_69_64_69_72_2E_5C_26_48_33_31_38_3A_20_43_69_64_64_69_20_67_F6_7A_20_68_61_73_61_72_69_6E_61_20_79_6F_6C_20_61_E7_61_72_2E_5C_26_50_32_36_34_3A_20_45_6C_6C_65_E7_6C_65_6D_65_64_65_6E_20_73_6F_6E_72_61_20_85_20_69_6C_65_20_69_79_69_63_65_20_79_69_6B_61_79_69_6E_2E_5C_26_50_32_38_30_3A_20_4B_6F_72_75_79_75_63_75_20_65_6C_64_69_76_65_6E_2F_6B_6F_72_75_79_75_63_75_20_6B_69_79_61_66_65_74_2F_67_F6_7A_20_6B_6F_72_75_79_75_63_75_2F_79_FC_7A_20_6B_6F_72_75_79_75_63_75_20_6B_75_6C_6C_61_6E_69_6E_2E_A0_5C_26_50_33_31_30_3A_20_48_65_6D_65_6E_A0_55_4C_55_53_41_4C_20_5A_45_48_49_52_20_44_41_4E_49_53_4D_41_20_4D_45_52_4B_45_5A_49_4E_49_4E_20_31_31_34_20_4E_4F_4C_55_20_54_45_4C_45_46_4F_4E_55_4E_55_A0_76_65_79_61_20_64_6F_6B_74_6F_72_75_2F_68_65_6B_69_6D_69_20_61_72_61_79_69_6E_2E_A0_5C_26_50_33_30_31_2B_50_33_31_32_3A_20_59_55_54_55_4C_44_55_47_55_4E_44_41_3A_20_4B_65_6E_64_69_6E_69_7A_69_20_69_79_69_20_68_69_73_73_65_74_6D_69_79_6F_72_73_61_6E_69_7A_A0_55_4C_55_53_41_4C_20_5A_45_48_49_52_20_44_41_4E_49_53_4D_41_20_4D_45_52_4B_45_5A_49_4E_49_4E_20_31_31_34_20_4E_4F_4C_55_20_54_45_4C_45_46_4F_4E_55_4E_55_A0_76_65_79_61_20_64_6F_6B_74_6F_72_75_2F_68_65_6B_69_6D_69_20_61_72_61_79_69_6E_2E_5C_26_50_33_30_35_2B_50_33_35_31_2B_50_33_33_38_3A_20_47_D6_5A_20_49_4C_45_20_54_45_4D_41_53_49_20_48_41_4C_49_4E_44_45_3A_20_53_75_20_69_6C_65_20_62_69_72_6B_61_E7_20_64_61_6B_69_6B_61_20_64_69_6B_6B_61_74_6C_69_63_65_20_64_75_72_75_6C_61_79_69_6E_2E_20_54_61_6B_69_6C_69_20_76_65_20_79_61_70_6D_61_73_69_20_6B_6F_6C_61_79_73_61_2C_20_6B_6F_6E_74_61_6B_20_6C_65_6E_73_6C_65_72_69_20_E7_69_6B_61_72_74_69_6E_2E_20_44_75_72_75_6C_61_6D_61_79_61_20_64_65_76_61_6D_20_65_64_69_6E_2E_5C_26_50_35_30_31_3A_20_49_E7_65_72_69_67_69_2F_6B_61_62_69_20_85_20_62_65_72_74_61_72_61_66_20_65_64_69_6E_2E
^FS
^FX FormNumber
^CF0,30
^FO600,630,0
^FB160,1,,C,
^FH
^FDF 540.006.2^FS
^XZ
You can chech zpl code in labelary.com/viewer.html
I am using RawPrinterHelper to send ZPL string to printer.
Can anyone help me please ?
To any poor soul like me that strangling to print an image using ZPL language, here what i have done.
int x = 300, y = 300;
pictogram0 = ZplImageConverter.GetZpl(ZplTools.ResizeImageData(ZplTools.RotateImageData(pictogramList[0]), x, y));
public class ZplImageConverter
{
public const int MaxBlacklimit = 765;
private static Dictionary<int, String> CompressMap
{
get
{
if (_CompressMap == null)
{
_CompressMap = new Dictionary<int, string>();
_CompressMap.Add(1, "G");
_CompressMap.Add(2, "H");
_CompressMap.Add(3, "I");
_CompressMap.Add(4, "J");
_CompressMap.Add(5, "K");
_CompressMap.Add(6, "L");
_CompressMap.Add(7, "M");
_CompressMap.Add(8, "N");
_CompressMap.Add(9, "O");
_CompressMap.Add(10, "P");
_CompressMap.Add(11, "Q");
_CompressMap.Add(12, "R");
_CompressMap.Add(13, "S");
_CompressMap.Add(14, "T");
_CompressMap.Add(15, "U");
_CompressMap.Add(16, "V");
_CompressMap.Add(17, "W");
_CompressMap.Add(18, "X");
_CompressMap.Add(19, "Y");
_CompressMap.Add(20, "g");
_CompressMap.Add(40, "h");
_CompressMap.Add(60, "i");
_CompressMap.Add(80, "j");
_CompressMap.Add(100, "k");
_CompressMap.Add(120, "l");
_CompressMap.Add(140, "m");
_CompressMap.Add(160, "n");
_CompressMap.Add(180, "o");
_CompressMap.Add(200, "p");
_CompressMap.Add(220, "q");
_CompressMap.Add(240, "r");
_CompressMap.Add(260, "s");
_CompressMap.Add(280, "t");
_CompressMap.Add(300, "u");
_CompressMap.Add(320, "v");
_CompressMap.Add(340, "w");
_CompressMap.Add(360, "x");
_CompressMap.Add(380, "y");
_CompressMap.Add(400, "z");
}
return _CompressMap;
}
}
private static Dictionary<int, String> _CompressMap;
public static string GetZpl(byte[] imageData, bool useCompression = true)
{
var imgConverter = new ImageConverter();
var bmp = (Bitmap)imgConverter.ConvertFrom(imageData);
var bytesPerRow = bmp.Width / 8;
if (bmp.Width % 8 > 0)
bytesPerRow = (((int)(bmp.Width / 8)) + 1);
else
bytesPerRow = bmp.Width / 8;
var totalBytes = bytesPerRow * bmp.Height;
string hex = CreateMonochromeHex(bmp, bytesPerRow, totalBytes);
return string.Format("^GFA,{0},{0},{1},{2}", totalBytes, bytesPerRow, useCompression ? EncodeHexAscii(hex, bytesPerRow) : hex);
}
#region Helpers
private static string CreateMonochromeHex(Bitmap bmp, int bytesPerRow, int totalBytes)
{
var sb = new StringBuilder();
var graphics = Graphics.FromImage(bmp);
graphics.DrawImage(bmp, 0, 0);
var auxBinaryChar = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' };
int index = 0;
for (int h = 0; h < bmp.Height; h++)
{
for (int w = 0; w < bmp.Width; w++)
{
//Remove borders
if (h == 0 || w == bmp.Width - 1 || w == 0 || h == bmp.Height - 1)
{
auxBinaryChar[index] = '0';
index++;
}
else
{
var rgb = bmp.GetPixel(w, h);
char auxChar = '1';
/* Determine using Luma
* Source1 = http://web.archive.org/web/20141017085834/http://nicholas.piasecki.name:80/blog/2013/02/using-the-epl2-gw-command/
* Source2 = http://web.archive.org/web/20141025194839/https://en.wikipedia.org/wiki/Luma_(video)
*
* var threshold = 127;
* var color = bitmap.GetPixelx, y;
* var luminance = color.R * 0.3 + color.G * 0.59 + color.B * 0.11;
* dotsindex = luminance < threshold;
*
*/
//If color is not pure white then apply thermal heat to draw black otherwise do not apply thermal heat to draw nothing (white)
if ((rgb.R + rgb.G + rgb.B) >= ZplImageConverter.MaxBlacklimit)
auxChar = '0';
auxBinaryChar[index] = auxChar;
index++;
}
if (index == 8 || w == (bmp.Width - 1))
{
sb.Append(FourByteBinary(new String(auxBinaryChar)));
auxBinaryChar = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' };
index = 0;
}
}
sb.Append("\n");
}
return sb.ToString();
}
private static string FourByteBinary(string binaryStr)
{
return string.Format("{0:X2}", Convert.ToByte(binaryStr, 2));
}
private static string EncodeHexAscii(string hex, int bytesPerRow)
{
int maxlinea = bytesPerRow * 2;
var sbCode = new StringBuilder();
var sbLinea = new StringBuilder();
String previousLine = null;
int counter = 1;
char aux = hex[0];
bool firstChar = false;
for (int i = 1; i < hex.Length; i++)
{
if (firstChar)
{
aux = hex[i];
firstChar = false;
continue;
}
if (hex[i] == '\n')
{
if (counter >= maxlinea && aux == '0')
{
sbLinea.Append(",");
}
else if (counter >= maxlinea && aux == 'F')
{
sbLinea.Append("!");
}
else if (counter > 20)
{
int multi20 = (counter / 20) * 20;
int resto20 = (counter % 20);
sbLinea.Append(CompressMap[multi20]);
if (resto20 != 0)
{
sbLinea.Append(CompressMap[resto20] + aux);
}
else
{
sbLinea.Append(aux);
}
}
else
{
sbLinea.Append(CompressMap[counter] + aux);
}
counter = 1;
firstChar = true;
if (sbLinea.ToString().Equals(previousLine))
{
sbCode.Append(":");
}
else
{
sbCode.Append(sbLinea.ToString());
}
previousLine = sbLinea.ToString();
sbLinea.Length = 0;
continue;
}
if (aux == hex[i])
{
counter++;
}
else
{
if (counter > 20)
{
int multi20 = (counter / 20) * 20;
int resto20 = (counter % 20);
sbLinea.Append(CompressMap[multi20]);
if (resto20 != 0)
{
sbLinea.Append(CompressMap[resto20] + aux);
}
else
{
sbLinea.Append(aux);
}
}
else
{
sbLinea.Append(CompressMap[counter] + aux);
}
counter = 1;
aux = hex[i];
}
}
return sbCode.ToString();
}
#endregion
}
public static class ZplTools
{
public static string Normalize(string input)
{
if (string.IsNullOrEmpty(input))
return null;
var sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
var byteArray = Encoding.GetEncoding(1252).GetBytes(input.Substring(i, 1));
var hexString = BitConverter.ToString(byteArray);
sb.Append("_" + hexString.Replace("-", "_"));
}
return sb.ToString();
}
public static byte[] ConvertToMonochromeBitmap(byte[] imageData)
{
var converter = new System.Drawing.ImageConverter();
var image = (System.Drawing.Bitmap)converter.ConvertFrom(imageData);
return (byte[])converter.ConvertTo(image.ConvertToMonochromeTiff(), typeof(byte[]));
}
public static byte[] ResizeImageData(byte[] imageData, int with, int height)
{
var converter = new System.Drawing.ImageConverter();
using (var source = (System.Drawing.Image)converter.ConvertFrom(imageData))
{
using (var thumbnail = new System.Drawing.Bitmap(with, height))
{
using (var g = System.Drawing.Graphics.FromImage(thumbnail))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(source, 0, 0, with, height);
}
return (byte[])converter.ConvertTo(thumbnail, typeof(byte[]));
}
}
}
public static byte[] RotateImageData(byte[] imageData)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
using (var image = (System.Drawing.Image)converter.ConvertFrom(imageData))
{
image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipXY);
return (byte[])converter.ConvertTo(image, typeof(byte[]));
}
}
public static string GetImageZplUploadString(byte[] imageData, string imageName)
{
return string.Format("^XA~DYE:{2},P,P,{0},,{1}^XZ", imageData.Length, ZplTools.GetASCIHexString(imageData), imageName);
}
public static string GetASCIHexString(byte[] data)
{
StringBuilder hex = new StringBuilder();
foreach (byte b in data)
hex.AppendFormat("{0:X2}", b);
return hex.ToString();
}
}
I don't know why but adding a ^FWN command to the top of the ZPL solved the problem.
Related
How to get Tensorflow PoseNet working with Xamarin forms/Xam.Android
I've tried with java/Kotlin android and then using it as a .jar but cant get it to work. I have this code in Java, and i need to try to replicate it using C# for Xamarin/Xamarin.Android JAVA METHOD fun estimateSinglePose(bitmap: Bitmap): Person { var t1: Long = SystemClock.elapsedRealtimeNanos() val inputArray = arrayOf(initInputArray(bitmap)) var t2: Long = SystemClock.elapsedRealtimeNanos() Log.i("posenet", String.format("Scaling to [-1,1] took %.2f ms", 1.0f * (t2 - t1) / 1_000_000)) val outputMap = initOutputMap(interpreter!!) t1 = SystemClock.elapsedRealtimeNanos() interpreter!!.runForMultipleInputsOutputs(inputArray, outputMap) t2 = SystemClock.elapsedRealtimeNanos() Log.i("posenet", String.format("Interpreter took %.2f ms", 1.0f * (t2 - t1) / 1_000_000)) val heatmaps = outputMap[0] as Array<Array<Array<FloatArray>>> val offsets = outputMap[1] as Array<Array<Array<FloatArray>>> val height = heatmaps[0].size val width = heatmaps[0][0].size val numKeypoints = heatmaps[0][0][0].size // Finds the (row, col) locations of where the keypoints are most likely to be. val keypointPositions = Array(numKeypoints) { Pair(0, 0) } for (keypoint in 0 until numKeypoints) { var maxVal = heatmaps[0][0][0][keypoint] var maxRow = 0 var maxCol = 0 for (row in 0 until height) { for (col in 0 until width) { heatmaps[0][row][col][keypoint] = sigmoid(heatmaps[0][row][col][keypoint]) if (heatmaps[0][row][col][keypoint] > maxVal) { maxVal = heatmaps[0][row][col][keypoint] maxRow = row maxCol = col } } } keypointPositions[keypoint] = Pair(maxRow, maxCol) } // Calculating the x and y coordinates of the keypoints with offset adjustment. val xCoords = IntArray(numKeypoints) val yCoords = IntArray(numKeypoints) val confidenceScores = FloatArray(numKeypoints) keypointPositions.forEachIndexed { idx, position -> val positionY = keypointPositions[idx].first val positionX = keypointPositions[idx].second yCoords[idx] = ( position.first / (height - 1).toFloat() * bitmap.height + offsets[0][positionY][positionX][idx] ).toInt() xCoords[idx] = ( position.second / (width - 1).toFloat() * bitmap.width + offsets[0][positionY] [positionX][idx + numKeypoints] ).toInt() confidenceScores[idx] = heatmaps[0][positionY][positionX][idx] } val person = Person() val keypointList = Array(numKeypoints) { KeyPoint() } var totalScore = 0.0f enumValues<BodyPart>().forEachIndexed { idx, it -> keypointList[idx].bodyPart = it keypointList[idx].position.x = xCoords[idx] keypointList[idx].position.y = yCoords[idx] keypointList[idx].score = confidenceScores[idx] totalScore += confidenceScores[idx] } person.keyPoints = keypointList.toList() person.score = totalScore / numKeypoints return person } I have got the PoseNet model running in a C# method but this method is written for object detection and not returning Keypoints, i don't know how to adapt it to get the keypoints C# Method public void Recognize(int[] colors) { if (!initialized) { throw new Exception("Initialize TensorflowLiteService first"); } MessagingCenter.Instance.Send(this, nameof(AR.InputTensorMessage), new InputTensorMessage() { Colors = colors, }); using (var op = new BuildinOpResolver()) { using (var interpreter = new Interpreter(model, op)) { InvokeInterpreter(colors, interpreter); } } } private void InvokeInterpreter(int[] colors, Interpreter interpreter) { if (useNumThreads) { interpreter.SetNumThreads(Environment.ProcessorCount); } var allocateTensorStatus = interpreter.AllocateTensors(); if (allocateTensorStatus == Status.Error) { throw new Exception("Failed to allocate tensor"); } var input = interpreter.GetInput(); using (var inputTensor = interpreter.GetTensor(input[0])) { CopyColorsToTensor(inputTensor.DataPointer, colors); var watchInvoke = Stopwatch.StartNew(); interpreter.Invoke(); watchInvoke.Stop(); Console.WriteLine($"InterpreterInvoke: {watchInvoke.ElapsedMilliseconds}ms"); } var output = interpreter.GetOutput(); var outputIndex = output[0]; var outputTensors = new Tensor[output.Length]; for (var i = 0; i < output.Length; i++) { outputTensors[i] = interpreter.GetTensor(outputIndex + i); } var detection_boxes_out = outputTensors[0].GetData() as float[]; var detection_classes_out = outputTensors[1].GetData() as float[]; var detection_scores_out = outputTensors[2].GetData() as float[]; var num_detections_out = outputTensors[3].GetData() as float[]; var numDetections = num_detections_out[0]; LogDetectionResults(detection_classes_out, detection_scores_out, detection_boxes_out, (int)numDetections); }
Extracting pdf images in a correct order iTextSharp
I'm trying to extract images from a PDF File, but I really need to have it at the correct order to get the correct image. static void Main(string[] args) { string filename = "D:\\910723575_marca_coletiva.pdf"; PdfReader pdfReader = new PdfReader(filename); var imagemList = ExtraiImagens(pdfReader); // converter byte[] para um bmp List<Bitmap> bmpSrcList = new List<Bitmap>(); IList<byte[]> imagensProcessadas = new List<byte[]>(); foreach (var imagem in imagemList) { System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(); try { Image img = (Image)converter.ConvertFrom(imagem); ConsoleWriteImage(img); imagensProcessadas.Add(imagem); } catch (Exception) { continue; } } System.Console.ReadLine(); } public static void ConsoleWriteImage(Image img) { int sMax = 39; decimal percent = Math.Min(decimal.Divide(sMax, img.Width), decimal.Divide(sMax, img.Height)); Size resSize = new Size((int)(img.Width * percent), (int)(img.Height * percent)); Func<System.Drawing.Color, int> ToConsoleColor = c => { int index = (c.R > 128 | c.G > 128 | c.B > 128) ? 8 : 0; index |= (c.R > 64) ? 4 : 0; index |= (c.G > 64) ? 2 : 0; index |= (c.B > 64) ? 1 : 0; return index; }; Bitmap bmpMin = new Bitmap(img, resSize.Width, resSize.Height); Bitmap bmpMax = new Bitmap(img, resSize.Width * 2, resSize.Height * 2); for (int i = 0; i < resSize.Height; i++) { for (int j = 0; j < resSize.Width; j++) { Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMin.GetPixel(j, i)); Console.Write("██"); } Console.BackgroundColor = ConsoleColor.Black; Console.Write(" "); for (int j = 0; j < resSize.Width; j++) { Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2, i * 2)); Console.BackgroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2, i * 2 + 1)); Console.Write("▀"); Console.ForegroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2 + 1, i * 2)); Console.BackgroundColor = (ConsoleColor)ToConsoleColor(bmpMax.GetPixel(j * 2 + 1, i * 2 + 1)); Console.Write("▀"); } System.Console.WriteLine(); } } public static IList<byte[]> ExtraiImagens(PdfReader pdfReader) { var data = new byte[] { }; IList<byte[]> imagensList = new List<byte[]>(); for (int numPag = 1; numPag <= 3; numPag++) //for (int numPag = 1; numPag <= pdfReader.NumberOfPages; numPag++) { var pg = pdfReader.GetPageN(numPag); var res = PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)) as PdfDictionary; var xobj = PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)) as PdfDictionary; if (xobj == null) continue; var keys = xobj.Keys; if (keys == null) continue; PdfObject obj = null; PdfDictionary tg = null; for (int key = 0; key < keys.Count; key++) { obj = xobj.Get(keys.ElementAt(key)); if (!obj.IsIndirect()) continue; tg = PdfReader.GetPdfObject(obj) as PdfDictionary; obj = xobj.Get(keys.ElementAt(key)); if (!obj.IsIndirect()) continue; tg = PdfReader.GetPdfObject(obj) as PdfDictionary; var type = PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)) as PdfName; if (!PdfName.IMAGE.Equals(type)) continue; int XrefIndex = (obj as PRIndirectReference).Number; var pdfStream = pdfReader.GetPdfObject(XrefIndex) as PRStream; data = PdfReader.GetStreamBytesRaw(pdfStream); imagensList.Add(PdfReader.GetStreamBytesRaw(pdfStream)); } } return imagensList; } } The method ConsoleWriteImage is only to print the image at the console and I used it to study the behavior of the order that iTextSharp was retrieving it for me , based on my code. Any help ?
Unfortunately the OP has not explained what the correct order is - this is not self-explanatory because there might be certain aspects of a PDF which are not obvious for a program, merely for a human reader viewing the rendered PDF. At least, though, it is likely that the OP wants to get his images on a page-by-page basis. This obviously is not what his current code provides: His code scans the whole base of objects inside the PDF for image objects, so he will get image objects, but the order may be completely random; in particular he may even get images contained in the PDF but not used on any of its pages... To retrieve images on a page-by-page order (and only images actually used), one should use the parser framework, e.g. PdfReader reader = new PdfReader(pdf); PdfReaderContentParser parser = new PdfReaderContentParser(reader); MyImageRenderListener listener = new MyImageRenderListener(); for (int i = 1; i <= reader.NumberOfPages; i++) { parser.ProcessContent(i, listener); } // Process images in the List listener.MyImages // with names in listener.ImageNames (Excerpt from the ExtractImages.cs iTextSharp example) where MyImageRenderListener is defined to collect images: public class MyImageRenderListener : IRenderListener { /** the byte array of the extracted images */ private List<byte[]> _myImages; public List<byte[]> MyImages { get { return _myImages; } } /** the file names of the extracted images */ private List<string> _imageNames; public List<string> ImageNames { get { return _imageNames; } } public MyImageRenderListener() { _myImages = new List<byte[]>(); _imageNames = new List<string>(); } [...] public void RenderImage(ImageRenderInfo renderInfo) { try { PdfImageObject image = renderInfo.GetImage(); if (image == null || image.GetImageBytesType() == PdfImageObject.ImageBytesType.JBIG2) return; _imageNames.Add(string.Format("Image{0}.{1}", renderInfo.GetRef().Number, image.GetFileType() ) ); _myImages.Add(image.GetImageAsBytes()); } catch { } } [...] } (Excerpt from MyImageRenderListener.cs iTextSharp example) The ImageRenderInfo renderInfo furthermore also contains information on location and orientation of the image on the page in question which might help to deduce the correct order the OP is after.
How to convert macro CV_IMAGE_ELEM() in c++ into c# using opencvsharp wrapper
hi i'm using visual studio 2008 and c++ to implement the watershed algorithm it works well.But i'm recently working on converting the same code into c# using opencvsharp wrapper class in visual studio 2010.I completed most of the code but i couldn't convertCV_IMAGE_ELEM() into opencvsharp can anyone please help this is my full c++ source code code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/watershed.cpp?rev=493 and this is where i get the trouble // paint the watershed image for( i = 0; i < markers->height; i++ ) for( j = 0; j < markers->width; j++ ) { int idx = CV_IMAGE_ELEM( markers, int, i, j );//markersIPL_DEPTH_32S uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );//BGR,j*3 if( idx == -1 ) //-1? dst[0] = dst[1] = dst[2] = (uchar)255; else if( idx <= 0 || idx > comp_count ) // dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here else // { uchar* ptr = color_tab->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } Is their any other way to do the job without using CV_IMAGE_ELEM? error is "the name does not exist in the current context" here is the c# code I've been working on i did this using opencvsharp, i cant complete the code any help using (IplImage img0 = new IplImage("1180.jpg", LoadMode.AnyDepth | LoadMode.AnyColor)){ using (IplImage img = img0.Clone()) using (IplImage marker_mask = new IplImage(img0.Size, BitDepth.U8, 1)) using (IplImage markers = new IplImage(img.Size, BitDepth.S32, 1)) using (IplImage img_gray = img0.Clone()) using (IplImage wshed = img0.Clone()){ wshed.Zero(); marker_mask.Zero(); using (CvWindow w_image = new CvWindow("image", WindowMode.AutoSize, img)) { CvPoint prev_pt = new CvPoint(-1, -1); w_image.OnMouseCallback += delegate(MouseEvent ev, int x, int y, MouseEvent flags) { if (ev == MouseEvent.LButtonUp || (flags & MouseEvent.FlagLButton) == 0) { prev_pt = new CvPoint(-1, -1); } else if (ev == MouseEvent.LButtonDown) { prev_pt = new CvPoint(x, y); } else if (ev == MouseEvent.MouseMove && (flags & MouseEvent.FlagLButton) != 0) { CvPoint pt = new CvPoint(x, y); if (prev_pt.X < 0) { prev_pt = pt; } marker_mask.Line(prev_pt,pt,Cv.ScalarAll(255),5,LineType.Link8,0); img.Line(prev_pt,pt,Cv.ScalarAll(255),5,LineType.Link8,0); prev_pt = pt; w_image.ShowImage(img); } }; for (; ; ) { switch (CvWindow.WaitKey(0)) { case 27: return; case 'r': marker_mask.Zero(); img0.Copy(img); w_image.ShowImage(img); break; case 'w': case '\r': CvSeq<CvPoint> contours; CvMat color_tab=null; int i,j,comp_count=0; CvMemStorage storage = new CvMemStorage(); Cv.FindContours(marker_mask,storage, out contours); markers.Zero(); for(; contours !=null; contours=contours.HNext,comp_count++){ Cv.DrawContours(markers, contours, Cv.ScalarAll(comp_count+1), Cv.ScalarAll(comp_count+1),-1,-1,LineType.Link8,new CvPoint(0,0)); } if(comp_count ==0){ continue; color_tab=Cv.CreateMat(1,comp_count,MatrixType.U8C3); for(i=0;i<comp_count;i++){ /* uchar* ptr = color_tab->data.ptr + i*3; ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50); */ unsafe{ CvRNG rng = new CvRNG(); byte* ptr = (byte*)color_tab.DataArrayByte.Ptr +i*3; ptr[0] = (byte)(Cv.RandInt(rng)%180 + 50); ptr[1] = (byte)(Cv.RandInt(rng)%180 + 50); ptr[2] = (byte)(Cv.RandInt(rng)%180 + 50); } /* CvRNG rng = new CvRNG(); for (i = 0; i < comp_count * 3; i++) ptr[0] = (byte)(Cv.RandInt(rng)%180 + 50); ptr[1] = (byte)(Cv.RandInt(rng)%180 + 50); ptr[2] = (byte)(Cv.RandInt(rng)%180 + 50); //for (i = 0; i < comp_count * 3; i++) // ptr[i] = (byte)(cvRandInt(...) % 180 + 50); */ } { double t =(double)Cv.GetTickCount(); Cv.Watershed(img0,markers); Cv.Save(markers,"img0.xml"); t=(double)Cv.GetTickCount()-t; Console.WriteLine("exec time = %gms\n",t/(Cv.GetTickFrequency()*1000)); } // paint the watershed image for(i=0;i<markers.Height;i++) for(j=0;i<markers.Width;j++) { int idx = CV_IMAGE_ELEM( markers, int, i, j );//markersIPL_DEPTH_32S uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );//BGR,j*3 if( idx == -1 ) //-1? dst[0] = dst[1] = dst[2] = (uchar)255; else if( idx <= 0 || idx > comp_count ) // dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here else // { uchar* ptr = color_tab->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } Cv.AddWeighted(wshed,0.5,img_gray,0.5,0,wshed); Cv.ShowImage("watershed transform",wshed); Cv.ReleaseMat(color_tab); } } return 0; i'm struggling days to find a answer any help will be appreciated. thank you.
how can i drag a 2d level array from a txt file
i am working on improving this tower defence gtame that i finished from this tutorial http://xnatd.blogspot.com/ i now wish to load the levels from a text file but not quite sure how i would do this using a streamreader, any tips? heres the source code for my level class; public class Level { protected int temperature; protected int levelNo; private Queue<Vector2> waypoints = new Queue<Vector2>(); public Queue<Vector2> Waypoints { get { return waypoints; } } int[,] map = new int[,] { {0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,}, {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,}, {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,}, {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,}, {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,}, {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,}, {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,}, {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,}, {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,}, {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,}, {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,}, {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,}, {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,}, {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,}, {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,}, {0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,}, }; public int Temperature // get the width of the level (how many numbers are in a row) { get { return temperature; } } public int LevelNo // get the width of the level (how many numbers are in a row) { get { return levelNo; } set { levelNo = value; } } public int Width // get the width of the level (how many numbers are in a row) { get { return map.GetLength(1); } } public int Height // get the height of our level (how many numbers are there in a column) { get { return map.GetLength(0); } } public int GetIndex(int cellX, int cellY) // return the index of the requested cell. { if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1) { return 0; } else { return map[cellY, cellX]; } } public List<Texture2D> tileTextures = new List<Texture2D>(); // list to contain all the textures /// <summary> /// CONSTRUCTOR NEW LEVEL /// </summary> public Level() { SetWayPoints(map); this.temperature = 1000; this.levelNo = 2; } private void SetWayPoints(int[,] map) { int currentPosVal = map[0, 0]; int lPos = 0; int rPos = 0; int uPos = 0; int dPos = 0; int storedXPos = 99; int storedYPos = 99; int endstoredXPos = 99; int endstoredYPos = 99; int lastY = 0; int lastX = 0; //Search top ROW for start for (int i = 0; i < Width; i++) { currentPosVal = map[0, i]; if (currentPosVal == 1) { storedXPos = i; storedYPos = 0; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); lastX = storedXPos; lastY = storedYPos; break; } } //if start not set if (storedXPos == 99 && storedXPos == 99) { //look in 1st coloum for start for (int i = 0; i < Height; i++) { currentPosVal = map[i, 0]; if (currentPosVal == 1) { storedXPos = 0; storedYPos = i; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); lastX = storedXPos; lastY = storedYPos; break; } } } //search end COLOUM for end for (int i = 0; i < Height; i++) { currentPosVal = map[i, Width - 1]; if (currentPosVal == 1) { endstoredXPos = Width - 1; endstoredYPos = i; } } //If end not set look in bottom row for end if (endstoredXPos == 99 && endstoredYPos == 99) { for (int i = 0; i < Width; i++) { currentPosVal = map[7, i]; if (currentPosVal == 1) { endstoredXPos = i; endstoredYPos = Height - 1; } } if (endstoredXPos == 99 && endstoredYPos == 99) { } } // start midlle loop while (true) { lPos = 0; rPos = 0; uPos = 0; dPos = 0; //If current pos is not down the left hand edge if (storedXPos > 0) { lPos = map[storedYPos, storedXPos - 1]; } //If current pos square is not down the right hand edge if (storedXPos < Width - 1) { rPos = map[storedYPos, storedXPos + 1]; } //If current pos square is not in the top row if (storedYPos > 0) { uPos = map[storedYPos - 1, storedXPos]; } //If current pos square is not in the bottom row if (storedYPos < Height - 1) { dPos = map[storedYPos + 1, storedXPos]; } if (lPos == 1 && (lastX != storedXPos - 1 || lastY != storedYPos)) { lastX = storedXPos; lastY = storedYPos; storedXPos--; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); } else if (rPos == 1 && (lastX != storedXPos + 1 || lastY != storedYPos)) { lastX = storedXPos; lastY = storedYPos; storedXPos++; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); } else if (dPos == 1 && (lastX != storedXPos || lastY != storedYPos + 1)) { lastX = storedXPos; lastY = storedYPos; storedYPos++; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); } else if (uPos == 1 && (lastX != storedXPos || lastY != storedYPos - 1)) { lastX = storedXPos; lastY = storedYPos; storedYPos--; waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32); } if (storedXPos == endstoredXPos && storedYPos == endstoredYPos) { break; } } } public void AddTexture(Texture2D texture) // method adds a texture to our texture list. { tileTextures.Add(texture); } //Reads number from array, store its value in textureIndex, Use textureIndex to get the texture from tileTextures, public void Draw(SpriteBatch batch) //Draw appropiate texture, Repeat through all elements of the array { int textureIndex; Texture2D texture; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (levelNo == 0) { textureIndex = map[y, x]; if (textureIndex == -1) continue; texture = tileTextures[textureIndex]; batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White); } if (levelNo > 0) { textureIndex = map[y, x]; textureIndex += (levelNo * 2); if (textureIndex == -1) continue; texture = tileTextures[textureIndex]; batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White); } } } } } }
Since C# is compiled and can't do evals like scripted languages can (not that you should be doing that anyway) you should probably use streamreader to read the data from file (formatted perhaps as delimited text: csv or tsv) Assuming you're loading something similar to the map you have up there then a map file could look something like 0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0 0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0 0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0 0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0 0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0 0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0 0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0 0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0 0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0 0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0 0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0 0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0 0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0 0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0 0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1 where you would then loop through the file and read each line As each line is read in, you can then split the line by "," to create a 1d array which can then be assigned to an index in the 2d array. Loop for each line to create the entire 2d map array Take a look at this if you need help splitting strings in C#; note that the sample code splits at spaces " " and that you should use s.Split(','); for csv
With a file that small i would use: File.ReadAllLines , and after readling it loop over the lines with a foreach loop.
reading a barcode with a webcam
Hey, I'm trying to read a EAN-13 barcode from my webcam. I already wrote a class to do that work. I'm taking a picture from my webcam, trimming it to show ONLY the barcode, and reading the barcode with the code tables from wikipedia. For some reason, the barcode gets trimmed, but the output is always "0-1-1-1-1-1-1-1-1-1-1-1-1". I wonder if i did any stupid mistake or misunderstood something? I do not want to use ANY third-party programs! this is my code for now: public class BarcodeDecoder { static string[] ligerade = new string[] { "0100111", "0110011", "0011011", "0100001", "0011101", "0000101", "0010001", "0001001", "0010111" }; static string[] rechtsgerade = new string[ligerade.Length]; static string[] liungerade = new string[ligerade.Length]; static string[] GeradeUG = new string[] { "UUUUUU", "UUGUGG", "UUGGUG", "UUGGGU", "UGUUGG", "UGGUUG", "UGGGUU", "UGUGUG", "UGUGGU", "UGGUGU" }; static int[] links; static int[] rechts; static string result; public static string Decode(Bitmap b) { result = ""; Bitmap bb = CutOutOf(b, b.Height / 2); bb = trimBitmap(bb); int[] lgs = GetNumberOutOf(bb); int[][] rr = trimArray(lgs); links = rr[0]; rechts = rr[1]; FillArrays(); BearbeiteLinks(); BearbeiteRechts(); return result; } static void BearbeiteLinks() { string GU = ""; string[] zahlen = new string[6]; zahlen[0] = OutOfArray(links, 0, 7); zahlen[1] = OutOfArray(links, 7, 7); zahlen[2] = OutOfArray(links, 14, 7); zahlen[3] = OutOfArray(links, 21, 7); zahlen[4] = OutOfArray(links, 28, 7); zahlen[5] = OutOfArray(links, 35, 7); foreach (string pq in zahlen) { bool gerade = ligerade.ToList().IndexOf(pq) > -1; if (gerade) { result += ligerade.ToList().IndexOf(pq).ToString(); GU += "G"; } else { result += liungerade.ToList().IndexOf(pq).ToString(); GU += "U"; } } result = GeradeUG.ToList().IndexOf(GU).ToString() + result; } static void BearbeiteRechts() { string[] zahlen = new string[6]; zahlen[0] = OutOfArray(rechts, 0, 7); zahlen[1] = OutOfArray(rechts, 7, 7); zahlen[2] = OutOfArray(rechts, 14, 7); zahlen[3] = OutOfArray(rechts, 21, 7); zahlen[4] = OutOfArray(rechts, 28, 7); zahlen[5] = OutOfArray(rechts, 35, 7); foreach (string pq in zahlen) { result += rechtsgerade.ToList().IndexOf(pq).ToString(); } } static string OutOfArray(int[] ar, int startindex, int length) { int[] gar = new int[length]; Array.Copy(ar, startindex, gar, 0, length); StringBuilder bilder = new StringBuilder(); for (int i = 0; i < gar.Length; i++) { bilder.Append(gar[i].ToString()); } return bilder.ToString(); } static Bitmap trimBitmap(Bitmap b) { bool alreadyBlack = false; int firstblack = 0; for (int i = 0; i < b.Width; i++) { Color gp = b.GetPixel(i, 0); if ((gp.R + gp.G + gp.B) / 3 < 128) { if (!alreadyBlack) { alreadyBlack = true; firstblack = i; } } } bool alreadyblack = false; int lastblack = 0; for (int i = b.Width -1; i > 0; i--) { Color gpp = b.GetPixel(i, 0); if ((gpp.R + gpp.G + gpp.B) / 3 < 128) { if (!alreadyblack) { alreadyblack = true; lastblack = i; } } } Bitmap result = new Bitmap(lastblack - firstblack, 1); for (int i = firstblack; i < lastblack; i++) { Color c = b.GetPixel(i, 0); result.SetPixel(i - firstblack, 0, c); } result.Save("C:\\result.bmp", System.Drawing.Imaging.ImageFormat.Bmp); return result; } static int[][] trimArray(int[] ar) { int[][] res = new int[2][]; int[] resl = new int[6 * 7]; int[] resr = new int[6 * 7]; Array.Copy(ar, 2, resl, 0, 6 * 7); Array.Copy(ar, 2 + 6 * 7 + 5, resr, 0, 6 * 7); res[0] = resl; res[1] = resr; return res; } static void FillArrays() { for (int i = 0; i < ligerade.Length; i++) { rechtsgerade[i] = string.Concat(ligerade[i].Reverse()); } for (int x = 0; x < liungerade.Length; x++) { liungerade[x] = Invert(rechtsgerade[x]); } } static string Invert(string xx) { string xs = ""; for (int y = 0; y < xx.Length; y++) { int fd = int.Parse(xx[y].ToString()); if (fd == 0) fd = 1; else fd = 0; xs += fd.ToString(); } return xs; } static Bitmap CutOutOf(Bitmap b, int y) { Bitmap res = new Bitmap(b.Width, 1); for (int i = 0; i < b.Width; i++) { Color c = b.GetPixel(i, y); res.SetPixel(i, 0, c); } return res; } static int[] GetNumberOutOf(Bitmap bb) { List<int> intlst = new List<int>(); float f = (float)bb.Width / 95.0f; float wd = f / 2.0f; for (float i = wd; i < bb.Width; i+=f) { Color c = bb.GetPixel((int)Math.Round(i,0), 0); intlst.Add(GetOutOfColor(c)); } return intlst.ToArray(); } static int GetOutOfColor(Color c) { if (c.A + c.B + c.R > 128 * 3) { return 0; } return 1; } } Sorry for german names in the code!
I see two problems: 1) You only scan the top most pixel row of your image (see the second parameter of GetPixel). Your barcode is probably in the middle of the image and not at the top. Color c = bb.GetPixel((int)Math.Round(i,0), 0); 2) Instead of the green component, you take the alpha component to convert the colored pixel into a binary value. Since the alpha component is probably always 255, you always get 0 unless you have a very dark pixel. if (c.A + c.B + c.R > 128 * 3)