| | |
Graphical Barcode Control
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Solved Threads: 0
i have build a user control in C# 2.0 using a panel and the 2Dgraphics namespace members. it renders perfectly until i do a matrix rotation. whilst in design mode, with the control on a test form, the rotation works like it should, but when i grab the control's handles to resize it, the graphics do not stay put inside the control. they tend to drag around by some seemingly proportional factor.
i have tried many ways around this to no avail...hoping you may have some new ideas i haven't tried.
thanks in advance
here's most of the rubber-meets-the-road code:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (orient != Orient.Up)
{
panel1_Paint_Oriented(sender, e);
}//if(invertImage)
else
{
// Set up the rotation transformation
// Rotate about the middle of the page by the associated orientation degrees
System.Drawing.Drawing2D.Matrix m = new Matrix();
//Point MiddleOfPage = new Point( iMiddle_Horizontal, iMiddle_Vertical );
Point MiddleOfPage = new Point(0,iMiddle_Vertical);
m.RotateAt( angle, MiddleOfPage, MatrixOrder.Append );
e.Graphics.Transform = m;
String intercharacterGap = "0";
//surround the incoming string (barcode.code) with the requisite "*" chars to delimit a barcode
String str = '*' + code.ToUpper() + '*';
int strLength = str.Length;
//parse the string for embedded invalid characters
for (int i = 0; i < code.Length; i++)
{
if (alphabet39.IndexOf(code[i]) == -1 || code[i] == '*')
{
e.Graphics.DrawString("INVALID BAR CODE TEXT", Font, Brushes.Red, 10, 10);
//bail
return;
}
}
//everything looks copesetic...
//calculate the horizontal and vertical drawing starting points ==================================================
//determine the horizontal offsets-------------------------------------------------------------------------
//build the encoded barcode from the normal text string
String encodedString = "";
for (int i = 0; i < strLength; i++)
{
if (i > 0)
{
//stuff in a separator character
encodedString += intercharacterGap;
}//if (i > 0)
//look into the array and add the 39 code for this character to the running barcode
encodedString += coded39Char[alphabet39.IndexOf(str[i])];
}//for ( int i=0;i<strLength;i++ )
//there will be a combination of 9 "0"'s and "1"'s, plus the separator char, for each normal char
int encodedStringLength = encodedString.Length;
int widthOfBarCodeString = 0;
double wideToNarrowRatio = 3;
//determine the relative width of the rendered barcode
if (horAlign != HorAlignType.Left)
{
//
for (int i = 0; i < encodedStringLength; i++)
{
if (encodedString[i] == '1')
widthOfBarCodeString += (int)(wideToNarrowRatio * (int)weight);
else
widthOfBarCodeString += (int)weight;
}//for ( int i=0;i<encodedStringLength; i++)
}//if (horAlign != HorAlignType.Left)
//initialize our barcode drawing offsets
int x = 0;
int wid = 0;
int yTop = 0;
//determine the enclosing rectangle for the header and footer
//header dims (height & width), depending on the font used
hSize = e.Graphics.MeasureString(headerText, headerFont);
//footer dims (height & width), depending on the font used
fSize = e.Graphics.MeasureString(code, footerFont);
//header & footer drawing offsets
int headerX = 0;
int footerX = 0;
if (horAlign == HorAlignType.Left)
{
//everything starts at the left margin
x = leftMargin;
headerX = leftMargin;
footerX = leftMargin;
}
else if (horAlign == HorAlignType.Center)
{
//half the difference between the barcode width and the control's width
x = (Width - widthOfBarCodeString) / 2;
//half the difference between the header width and the control's width
headerX = (Width - (int)hSize.Width) / 2;
//half the difference between the footer width and the control's width
footerX = (Width - (int)fSize.Width) / 2;
}
else
{
//right-justified
//the whole diff between the barcode and control widths
x = Width - widthOfBarCodeString;// - leftMargin;
headerX = Width - (int)hSize.Width;// - leftMargin;
footerX = Width - (int)fSize.Width;// - leftMargin;
}
//determine the barcode's vertical offset-----------------------------------------------------------------
if (showHeader)
{
//compensate for the header size and any prescribed top margin
yTop = (int)hSize.Height + topMargin;
//go ahead and draw the header while we're here...
e.Graphics.DrawString(headerText, headerFont, Brushes.Black, headerX, topMargin);
}
else
{
yTop = topMargin;
}
//build the complete barcode by drawing each vertical bar element as we go
for (int i = 0; i < encodedStringLength; i++)
{
if (encodedString[i] == '1')
wid = (int)(wideToNarrowRatio * (int)weight);
else
wid = (int)weight;
e.Graphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, yTop, wid, height);
x += wid;
}
yTop += height;
//cache the current status of the graphics
currentOrient = orient;
controlWidth = x + (LeftMargin * 2); //x=>width of rendered barcode
controlHeight = yTop; //top margin + header (if any) height + barcode height
if (showFooter)
{
e.Graphics.DrawString(code, footerFont, Brushes.Black, footerX, yTop);
//add the height of the footer to the overall graphics limits
controlHeight += (int)fSize.Height;
}//if
}
}
i have tried many ways around this to no avail...hoping you may have some new ideas i haven't tried.
thanks in advance
here's most of the rubber-meets-the-road code:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (orient != Orient.Up)
{
panel1_Paint_Oriented(sender, e);
}//if(invertImage)
else
{
// Set up the rotation transformation
// Rotate about the middle of the page by the associated orientation degrees
System.Drawing.Drawing2D.Matrix m = new Matrix();
//Point MiddleOfPage = new Point( iMiddle_Horizontal, iMiddle_Vertical );
Point MiddleOfPage = new Point(0,iMiddle_Vertical);
m.RotateAt( angle, MiddleOfPage, MatrixOrder.Append );
e.Graphics.Transform = m;
String intercharacterGap = "0";
//surround the incoming string (barcode.code) with the requisite "*" chars to delimit a barcode
String str = '*' + code.ToUpper() + '*';
int strLength = str.Length;
//parse the string for embedded invalid characters
for (int i = 0; i < code.Length; i++)
{
if (alphabet39.IndexOf(code[i]) == -1 || code[i] == '*')
{
e.Graphics.DrawString("INVALID BAR CODE TEXT", Font, Brushes.Red, 10, 10);
//bail
return;
}
}
//everything looks copesetic...
//calculate the horizontal and vertical drawing starting points ==================================================
//determine the horizontal offsets-------------------------------------------------------------------------
//build the encoded barcode from the normal text string
String encodedString = "";
for (int i = 0; i < strLength; i++)
{
if (i > 0)
{
//stuff in a separator character
encodedString += intercharacterGap;
}//if (i > 0)
//look into the array and add the 39 code for this character to the running barcode
encodedString += coded39Char[alphabet39.IndexOf(str[i])];
}//for ( int i=0;i<strLength;i++ )
//there will be a combination of 9 "0"'s and "1"'s, plus the separator char, for each normal char
int encodedStringLength = encodedString.Length;
int widthOfBarCodeString = 0;
double wideToNarrowRatio = 3;
//determine the relative width of the rendered barcode
if (horAlign != HorAlignType.Left)
{
//
for (int i = 0; i < encodedStringLength; i++)
{
if (encodedString[i] == '1')
widthOfBarCodeString += (int)(wideToNarrowRatio * (int)weight);
else
widthOfBarCodeString += (int)weight;
}//for ( int i=0;i<encodedStringLength; i++)
}//if (horAlign != HorAlignType.Left)
//initialize our barcode drawing offsets
int x = 0;
int wid = 0;
int yTop = 0;
//determine the enclosing rectangle for the header and footer
//header dims (height & width), depending on the font used
hSize = e.Graphics.MeasureString(headerText, headerFont);
//footer dims (height & width), depending on the font used
fSize = e.Graphics.MeasureString(code, footerFont);
//header & footer drawing offsets
int headerX = 0;
int footerX = 0;
if (horAlign == HorAlignType.Left)
{
//everything starts at the left margin
x = leftMargin;
headerX = leftMargin;
footerX = leftMargin;
}
else if (horAlign == HorAlignType.Center)
{
//half the difference between the barcode width and the control's width
x = (Width - widthOfBarCodeString) / 2;
//half the difference between the header width and the control's width
headerX = (Width - (int)hSize.Width) / 2;
//half the difference between the footer width and the control's width
footerX = (Width - (int)fSize.Width) / 2;
}
else
{
//right-justified
//the whole diff between the barcode and control widths
x = Width - widthOfBarCodeString;// - leftMargin;
headerX = Width - (int)hSize.Width;// - leftMargin;
footerX = Width - (int)fSize.Width;// - leftMargin;
}
//determine the barcode's vertical offset-----------------------------------------------------------------
if (showHeader)
{
//compensate for the header size and any prescribed top margin
yTop = (int)hSize.Height + topMargin;
//go ahead and draw the header while we're here...
e.Graphics.DrawString(headerText, headerFont, Brushes.Black, headerX, topMargin);
}
else
{
yTop = topMargin;
}
//build the complete barcode by drawing each vertical bar element as we go
for (int i = 0; i < encodedStringLength; i++)
{
if (encodedString[i] == '1')
wid = (int)(wideToNarrowRatio * (int)weight);
else
wid = (int)weight;
e.Graphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, yTop, wid, height);
x += wid;
}
yTop += height;
//cache the current status of the graphics
currentOrient = orient;
controlWidth = x + (LeftMargin * 2); //x=>width of rendered barcode
controlHeight = yTop; //top margin + header (if any) height + barcode height
if (showFooter)
{
e.Graphics.DrawString(code, footerFont, Brushes.Black, footerX, yTop);
//add the height of the footer to the overall graphics limits
controlHeight += (int)fSize.Height;
}//if
}
}
![]() |
Other Threads in the C# Forum
- Previous Thread: Monitoring a directory for changes - VS C# EE 2008
- Next Thread: OleDb in C# Express?
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion platform post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml





