Are vector based generators the best way to create barcodes? - c#

Is vector based generators the best way to generate barcodes? If yes, what are the namespaces that it will make use of? How is it used? Can anyone share some knowledge on this?

Assuming that we are talking about UPC like barcodes, vector based generation is not a must. It's the matter of representing some bits as vertical lines. So, you can easily do this using any graphic library or even using direct access to video buffer. You can represent a single bit with multiple pixels if you need a larger barcode. You don't need to use any interpolation I guess. But if you need a certain size (in pixels/centimeters etc.), vector based solution might be handful but still not a must.
C# source code example for generating scalable barcode graphics.
Steps:
1) Open a new C# Windows Forms sample project named BarCode.
2) Add a PictureBox and change BackColor to White and Dock to Fill.
3) Add Load and Resize events to Form1.
4) Copy & Paste the source code below over Form1.cs file.
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;
namespace BarCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool[] barCodeBits;
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
int numberOfBits = 100;
barCodeBits = new bool[numberOfBits];
for(int i = 0; i < numberOfBits; i++) {
barCodeBits[i] = (r.Next(0, 2) == 1) ? true : false;
}
Form1_Resize(null, null);
}
private void Form1_Resize(object sender, EventArgs e)
{
int w = pictureBox1.Width;
int h = pictureBox1.Height;
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(pictureBox1.Image);
Brush b = new SolidBrush(Color.Black);
for(int pos = 0; pos < barCodeBits.Length; pos++) {
if(barCodeBits[pos]) {
g.FillRectangle(b, ((float)pos / (float)barCodeBits.Length) * w, 0, (1.0f / (float)barCodeBits.Length) * w, h);
}
}
}
}
}

You don't have to develop barcodes using vector based graphics. I fact have a look at this link on codeproject as most of the work is already done for you. This genrates a bitmap of the required barcode.

Related

Tesseract OCR: very inaccurate result

Below is my very simple program to test Tesseract performance. The result I got was not as expected though the picture was a high quality and very clear screenshot (not a complex picture with colors). Please take a look at my code and the result below. I'm not sure if I did something wrong or the Tesseract engine can not handle this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using tessnet2;
namespace ImageProcessTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int up_lef_x = 1075;
int up_lef_y = 0070;
int bo_rig_x = 1430;
int bo_rig_y = 0095;
int width = bo_rig_x - up_lef_x;
int height = bo_rig_y - up_lef_y;
var bmpScreenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(
1075,
0070,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
// bmpScreenshot.Save("C:\\Users\\Exa\\Screenshot.png", ImageFormat.Png);
var image = bmpScreenshot;
var ocr = new Tesseract();
ocr.Init(#"C:\Users\Exa\Desktop\tessdata", "eng", false);
var result = ocr.DoOCR(image, Rectangle.Empty);
string result_str = "";
foreach (Word word in result)
result_str += word.Text;
MessageBox.Show(result_str);
}
}
}
96DPI screen shots are typically not adequate for OCR. As written in Tesseract wiki:
There is a minimum text size for reasonable accuracy. You have to consider resolution as well as point size. Accuracy drops off below 10pt x 300dpi, rapidly below 8pt x 300dpi. A quick check is to count the pixels of the x-height of your characters. (X-height is the height of the lower case x.) At 10pt x 300dpi x-heights are typically about 20 pixels, although this can vary dramatically from font to font. Below an x-height of 10 pixels, you have very little chance of accurate results, and below about 8 pixels, most of the text will be "noise removed".
However, if you know what exact font it is, you can try re-train tesseract to get better result.

Moving backwards through a gif much slower than moving forwards

I would like to be able to move through a gif frame by frame. In the example below, I use a trackbar to select which frame of a gif I want to see. For the designer I just dropped a PictureBox in the centre of the screen and stuck a TrackBar at the bottom of the screen.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace TestGifProject {
public partial class Form1 : Form {
private Image gif;
private FrameDimension fd;
public Form1() {
InitializeComponent();
gif = Image.FromFile("PATH\\TO\\SOME\\GIF.gif");
// just for this quick example...
this.Width = gif.Width + 20;
this.Height = gif.Height + 53;
pictureBox1.Width = gif.Width;
pictureBox1.Height = gif.Height;
pictureBox1.Image = gif;
fd = new FrameDimension(gif.FrameDimensionsList[0]);
trackBar1.SetRange(0, gif.GetFrameCount(fd) - 1);
}
private void trackBar1_MouseUp(object sender, MouseEventArgs e) {
gif.SelectActiveFrame(fd, trackBar1.Value);
pictureBox1.Image = gif;
}
}
}
When you move and release the trackbar, the frame is shown correctly however it is much slower when you scroll backwards rather than forwards (probably 10x faster forwards), often long enough to make it seem like the app has crashed. Is there anything I can do to speed up backwards scrolling through a gif?
One option is to create a list where you add each frame. SelectActiveFrame() could be resolving the correct image with a more complex logic, which could be the reason for slow action.
Create a new list to your class' root:
private List<Image> frames = new List<Image>();
Fill the list with the frame images:
for(int i = 0; i < gif.GetFrameCount(fd); i++)
{
gif.SelectActiveFrame(fd, i);
frames.Add((Image)gif.Clone());
}
Then use the list to set images to the PictureBox:
pictureBox1.Image = frames[trackBar1.Value];
I haven't tested this, but I think it should work.

How to to shrink(scale) an entire graphics structure?

I’m trying to fit a lot of rectangles into a Bitmap, which will be displayed in a picturebox. In my real code, I figure out the total width and height of a rectangle that can encompass all them, and then I divide that by the size of the Bitmap, to get my scaling factor. The problem is I can’t figure out how to perform the scaling. The code below is a simple version of what I need to do.
Please keep in mind that I cannot rely on the picturebox’s scaling abilities (stretch), and I don’t want to simply apply the scale to the width and height of all the rectangles, because in my real code it wouldn’t work very well. I need a way to shrink it down in Graphics. It is important the Bitmap stays the same size that it is (300 X 300). Thanks. The below code is what I've gotten so far, but nothing changes with the size.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap BM = new System.Drawing.Bitmap(300, 300);
Pen PenTest = new System.Drawing.Pen(Brushes.Red);
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics GR = Graphics.FromImage(BM))
{
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
// I need a scale of 0.60 in this example, because 300/500 = .6
GR.ScaleTransform(.6F, .6F);//Doesn't Work. No Change at all in the size of the rectangle.
}
pictureBox1.Image = BM;
}
}
}
Graphics.ScaleTransform performs the transformation but it does not draw anything.
You would need to then draw a rectangle after performing the transform on the graphics object:
using (Graphics GR = Graphics.FromImage(BM))
{
// ....
GR.ScaleTransform(.6F, .6F);
GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));
}

How to draw on screen for Windows Metro Style Apps in C#?

I simply want the user to be able to draw on the screen with some sort of pointer.
I already have the code working that captures the pointer's position, but I can't figure out how to place the pixels or shapes or whatever onto the screen.
I found this useful tutorial:
http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=137
And I've been looking at the documentation here:
http://msdn.microsoft.com/en-us/library/windows/apps/hh465055(v=VS.85).aspx
No luck so far. =( The tutorial is for Windows Phone 7, so it's a little bit different. =\ Help, please? =)
And this is what I have so far.
The drawing part:
private void Image_PointerPressed(object sender, PointerEventArgs e)
{
Debug.WriteLine("Image_PointerPressed");
isTracing = true;
}
private void Image_PointerReleased(object sender, PointerEventArgs e)
{
Debug.WriteLine("Image_PointerReleased");
isTracing = false;
}
private void Image_PointerMoved(object sender, PointerEventArgs e)
{
Debug.WriteLine("Image_PointerMoved");
Debug.WriteLine(e.GetCurrentPoint(this).Position);
if (isTracing)
{
Debug.WriteLine("isTracing");
Point pos = e.GetCurrentPoint(this).Position;
Color color = Colors.Green;
Line line = new Line() { X1 = pos.X, X2 = pos.X + 1, Y1 = pos.Y, Y2 = pos.Y + 1 };
line.Stroke = new SolidColorBrush(color);
line.StrokeThickness = 15;
//// So how do I draw this line onto the screen?? ////
}
}
For reference, stuff elsewhere in the code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Multimedia.FFmpeg;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Input;
using Windows.UI.Input;
bool isTracing = false;
Short form:
Add Lines and Rectangles to a panel
Manipulate a bitmap directly
Use an HTML5 Canvas element in a JavaScript/HTML project
Write the whole thing in C++/DirectX
There is no way in Metro/XAML to override an OnRender() method or the like. Your options are to add existing graphical elements (eg from the Shapes namespace) to a Canvas or other Panel, or to directly manipulate the pixels in a bitmap and push that bitmap into an Image element.
Metro/C# only has retained-mode graphics drawing, which means the only thing it will render is objects that have been added to the view hierarchy. What you're looking for is some kind of immediate-mode graphics drawing, eg
myCanvas.DrawLine( fromPoint, toPoint );
This can be done in a JavaScript/HTML project using HTML5's Canvas object. Which, sadly, is the way I'm leaning for such a project. It's unfortunate that Microsoft is not providing an immediate-mode element for XAML projects but that's the way it is. C++/DirectX is also an option for doing custom drawing but requires a substantial reworking of everything else that you're doing in the app.
Here is a great code sample on how to do this using XAML shapes.
http://www.codeproject.com/Articles/416878/Metro-Paint
You should add the Line to a UI element such as a Canvas.
The main problem in your code is that you're not attaching, the line to any XAML element i suggest you to do it to a Canvas element, more less like this:
newCanvas.Children.Add(line);
An alternative is to use Modern Components Drawing Library, it works on WinRT, uses .NET Graphics class like calls and draw directly on a XAML Canvas, note that if you want to save the image as a bitmap, you may need to use also WritableBitmapEx, as XAML Canvas can't be rendered to Bitmaps.
This sample project has code to draw on screen for Win 8 Store apps in C#/XAML:
http://code.msdn.microsoft.com/windowsapps/Drawing-on-a-Canvas-with-33510ae6
Here is the relevant C# file:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Input.Inking; //we need to add this name space in order to have many functions
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace DrawingOnCanvasWithInkPen
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
private uint _penID;
private uint _touchID;
private Point _previousContactPt;
private Point currentContactPt;
private double x1;
private double y1;
private double x2;
private double y2;
public MainPage()
{
this.InitializeComponent();
MyCanvas.PointerPressed += new PointerEventHandler(MyCanvas_PointerPressed);
MyCanvas.PointerMoved += new PointerEventHandler(MyCanvas_PointerMoved);
MyCanvas.PointerReleased += new PointerEventHandler(MyCanvas_PointerReleased);
MyCanvas.PointerExited += new PointerEventHandler(MyCanvas_PointerReleased);
}
#region PointerEvents
private void MyCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == _penID)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(MyCanvas);
// Pass the pointer information to the InkManager.
_inkKhaled.ProcessPointerUp(pt);
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
_touchID = 0;
_penID = 0;
// Call an application-defined function to render the ink strokes.
e.Handled = true;
}
private void MyCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == _penID)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas);
// Render a red line on the canvas as the pointer moves.
// Distance() is an application-defined function that tests
// whether the pointer has moved far enough to justify
// drawing a new line.
currentContactPt = pt.Position;
x1 = _previousContactPt.X;
y1 = _previousContactPt.Y;
x2 = currentContactPt.X;
y2 = currentContactPt.Y;
if (Distance(x1, y1, x2, y2) > 2.0) // We need to developp this method now
{
Line line = new Line()
{
X1 = x1,
Y1 = y1,
X2 = x2,
Y2 = y2,
StrokeThickness = 4.0,
Stroke = new SolidColorBrush(Colors.Green)
};
_previousContactPt = currentContactPt;
// Draw the line on the canvas by adding the Line object as
// a child of the Canvas object.
MyCanvas.Children.Add(line);
// Pass the pointer information to the InkManager.
_inkKhaled.ProcessPointerUpdate(pt);
}
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
}
private double Distance(double x1, double y1, double x2, double y2)
{
double d = 0;
d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
return d;
}
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Get information about the pointer location.
PointerPoint pt = e.GetCurrentPoint(MyCanvas);
_previousContactPt = pt.Position;
// Accept input only from a pen or mouse with the left button pressed.
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
// Pass the pointer information to the InkManager.
_inkKhaled.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
// Process touch input
}
}
#endregion
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
}
and xaml file:
<Page
x:Class="DrawingOnCanvasWithInkPen.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DrawingOnCanvasWithInkPen"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Canvas Name="MyCanvas" Background="White" HorizontalAlignment="Left" Height="513" Margin="83,102,0,0" VerticalAlignment="Top" Width="1056"/>
</Grid>
In its current state, it only processes pen or mouse input -- but I also got it to work for touch with only slight modifications.

drawing Graphics performance issue

i am trying to create slider movement graphics. The code explain better this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Black, 1F);
//timer for animation
private void Form1_Load(object sender, EventArgs e)
{
Timer a = new Timer();
a.Interval = 60;
a.Tick += new EventHandler(a_Tick);
a.Start();
}
void a_Tick(object sender, EventArgs e)
{
button1_Click(this, null);
}
//draws randomly generated point array.
int cnt = 0;
private void button1_Click(object sender, EventArgs e)
{
rAr();
g = pictureBox1.CreateGraphics();
g.Clear(Color.Violet);
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCurve(pen1, points2);
cnt++;
}
Random r = new Random();
Point[] p = new Point[100];
Point[] points2 = new Point[100];
int c = 4;
//fills new random point array
private void rAr()
{
points2.CopyTo(p, 0);
int cc = 1;
for (int i = points2.Length - 1; i > 0; i--)
{
points2[i - 1] = new Point(100 - cc, p[i].Y);
cc++;
}
points2[99] = new Point(100, r.Next(1, 50));
}
}
}
this code works fine put the problem is that it takes to much cpu and automation is not very smooth. is there some other way to achieve same thing put with much less cpu.
Thanks for replays
(Premature) Optimisations
In addition to the other answers already posted, you are attempting to draw a spline curve through 100 points.
This is probably a bad idea for two reasons: Firstly, fitting a curve to 100 points isn't a particularly fast thing to do under any circumstances. Secondly, as the points are all 1 pixel apart, there is likely to be no visual benefit to using a curve.
Therefore, there are several more things you could do that might improve the speed:
Try using a PolyLine rather than a curve,
If you need to use a curve, then you probably don't need to use 100 points across 100 pixels of the drawing - using 10 or 20 positions across that width may give you better results, and will significantly reduce the curve-fitting work that .net has to do.
You may be able to also remove some intermediate points that lie on the line/curve, so that you have fewer lines to draw. e.g. if you have points at (10,57) (11, 57) (12,57) then you can drop the middle point and just draw a straight line from (10,57) to (12,57).
Wrong Algorithm?
But wait! Before you optimise the code - is it actually the right solution to the problem?
It sounds as if the content is not meant to "change", but simply scroll sideways. In which case, only the new pixels introduced at one side of the image are actually "changing" with each frame - the rest simply move sideways. In that case, you can scroll (move) the region of the graphic that contains the "old" curve image, and then just draw the extra pixel or two that have "scrolled into view". Scrolling in this manner can be achieved in several different ways (e.g. blitting in the graphics, or if the content of the entire window is scrolling, by using windows Form scrolling commands)
In the same way, if the data is not changing, but simply "scrolling", then rebuilding the array for every frame is unnecessarily expensive. Using a circular buffer you could simply add a new point to the "end" of the array and delete one from the beginning without having to reallocate the array or copy all the intervening points at all.
So it's all about using the right tool for the job.
You should move all of the graphics code in Button1 to your picturebox's paint event, something like this:
private void button1_Click(object sender, EventArgs e)
{
rAr();
pictureBox1.Invalidate();
cnt++;
}
private void picturebox1_Paint(object sender PaintEventArgs e)
{
using graphics g = e.Graphics()
{
g.Clear(Color.Violet);
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawCurve(pen1, points2);
}
}

Categories

Resources