I need to print all inventory items Barcodes at once. For print, i've select all items by Button click and do print using DrawString or DrawImage function which works fine but the problem if name of items are too long then i have clip width to 100px and text got wrap and print function call but on printing 2nd line, the items starting Y points unable to identify which creates problem. if i identifying 1st row height correctly then i can set 2nd rows Y coordinates accordingly that problem can be resolved but i dont know how to do. As you can see result in below image.
*The problem is in this piece of code:
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
//MessageBox.Show(rectangleHeight.ToString());
startX = 5;
startY += 150; // space between 2 lines
numbarcode = 0;
}
code:
// The PrintPage event is raised for each page to be printed.
private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
{
int startX = 5;
int startY = 5;
Database db = new Database();
db.DBOpen();
int NBbarcode_perLine = 5;
int numbarcode = 0;
int barcodePerPage = 35;
int totalcodebar = listTobePrint.Count;
for (int i = 0; i < barcodePerPage; i++)
{
String code = listTobePrint[countbarcode].Code;
String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);
Font printFont = new Font("Arial", 10.0f);
e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
startX, startY, new StringFormat());
int x2 = startX + 3;
int y2 = startY + 15;
e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);
int x3 = startX;
int y3 = y2 + 50;
e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
x3, y3, new StringFormat());
int x4 = startX;
int y4 = y3 + 15;
e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
x4, y4, new StringFormat());
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(name, printFont);
int width = (int)stringSize.Width;
int height = (int)stringSize.Height;
//MessageBox.Show(width.ToString() + ", Height: "+height);
int x5 = startX;
int y5 = y4 + 15;
RectangleF rectangle = new RectangleF(x5, y5, 100, 0);
int rectangleWidht = (int)rectangle.Width;
int rectangleHeight = (int)rectangle.Height;
//MessageBox.Show(rectangleHeight.ToString());
e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
rectangle, new StringFormat());
numbarcode++;
countbarcode++;
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
//MessageBox.Show(rectangleHeight.ToString());
startX = 5;
startY += 150; // space between 2 lines
numbarcode = 0;
}
if (countbarcode == totalcodebar) break;
if (i == barcodePerPage - 1)
{
e.HasMorePages = true;
return;
}
}
countbarcode = 0;
e.HasMorePages = false;
db.DBClose();
}
one first solution is:
decrease one line of barcode and increase the space between two lines: (because you have the risk to have a cut line in bottom of page)
int barcodePerPage = 30;//instead of 35
and you increase the space betwween two line
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
//MessageBox.Show(rectangleHeight.ToString());
startX = 5;
startY += 200; // ***** space between 2 lines increased
numbarcode = 0;
}
this solution has the advantage to have the same number of barcode per page
another solution will be to calcul the number of char of next 35 barcode and following the number of char of item you choose the first way to print 35 barcode or if char is too long, the second way to print 30 barcode. if you dont know to do that i could help you
This solution could have different number of barcode per page
Another solution is to choose a little font for the item (arial 8 for example) and you could win space
Related
I would like to display the 2 different stacked area elements according to their parameters. But the chart area displays it not as specified and puts the second block at the top right corner of the first stacked area. They should be displayed side by side not stacked.
...
using System.Windows.Forms.DataVisualization.Charting;
namespace Gantt_Tool
{
public partial class ReadModel : Form
{
public ReadModel()
{
InitializeComponent();
CreateChart();
}
private void ReadModel_Load(object sender, EventArgs e)
{
}
private void CreateChart()
{
chart1.Series.Add($"a");
chart1.Series[$"a"].Points.Add(new DataPoint(0, 2));
chart1.Series[$"a"].Points.Add(new DataPoint(2, 2));
chart1.Series[$"a"].ChartType = SeriesChartType.StackedArea;
chart1.Series.Add($"b");
chart1.Series[$"b"].Points.Add(new DataPoint(2, 3));
chart1.Series[$"b"].Points.Add(new DataPoint(5, 3));
chart1.Series[$"b"].ChartType = SeriesChartType.StackedArea;
}
}
How can I set the blocks to a side by side order or placed freely? And how can I get unfilled rectangles?
Update: Here is an example of how it should look like:
From your comments, I take that you want to have a chart with freely-placed, unfilled rectangles and labels.
None of the MSChart types will do that.
Here is how to use a Point chart with a few lines of owner-drawing. Note how nicely this will behave when resizing the chart...
Here is the setup:
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
ax.Maximum = 9; // pick or calculate
ay.Maximum = 6; // minimum and..
ax.Interval = 1; // maximum values..
ay.Interval = 1; // .. needed
ax.MajorGrid.Enabled = false;
ay.MajorGrid.Enabled = false;
Series s1 = chart1.Series.Add("A");
s1.ChartType = SeriesChartType.Point;
Now we add your five boxes. I use a sepcial function that adds the points and stuffs the box size into the Tag of each point..:
AddBox(s1, 1, 0, 3, 1, "# 1");
AddBox(s1, 2, 1, 2, 2, "# 2");
AddBox(s1, 4, 0, 4, 2, "# 3");
AddBox(s1, 4, 2, 2, 2, "# 4");
AddBox(s1, 4, 4, 1, 1, "# 5");
int AddBox(Series s, float x, float y, float w, float h, string label)
{
return AddBox(s, new PointF(x, y), new SizeF(w, h), label);
}
int AddBox(Series s, PointF pt, SizeF sz, string label)
{
int i = s.Points.AddXY(pt.X, pt.Y);
s.Points[i].Tag = sz;
s.Points[i].Label = label;
s.Points[i].LabelForeColor = Color.Transparent;
s.Points[i].Color = Color.Transparent;
return i;
}
The drawing is also simple; only the use of the Axes function ValueToPixelPosition is noteworthy..:
private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
if (chart1.Series[0].Points.Count <= 0) return;
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
Graphics g = e.ChartGraphics.Graphics;
using (StringFormat fmt = new StringFormat()
{ Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center})
foreach (Series s in chart1.Series)
{
foreach (DataPoint dp in s.Points)
{
if (dp.Tag == null) break;
SizeF sz = (SizeF)dp.Tag;
double vx2 = dp.XValue + sz.Width;
double vy2 = dp.YValues[0] + sz.Height;
int x1 = (int)ax.ValueToPixelPosition(dp.XValue);
int y1 = (int)ay.ValueToPixelPosition(dp.YValues[0]);
int x2 = (int)ax.ValueToPixelPosition(vx2);
int y2 = (int)ay.ValueToPixelPosition(vy2);
Rectangle rect = Rectangle.FromLTRB(x1, y2, x2, y1);
using (Pen pen = new Pen(s.Color, 2f))
g.DrawRectangle(pen, rect);
g.DrawString(dp.Label, Font, Brushes.Black, rect, fmt);
}
}
}
Here is a little Linq to calculate the Minimum and Maximum values for the Axes to hold just the right size; chart won't do it by itself since the size in the tags of the points is not known...
private void setMinMax(Chart chart, ChartArea ca)
{
var allPoints = chart.Series.SelectMany(x => x.Points);
double minx = allPoints.Select(x => x.XValue).Min();
double miny = allPoints.Select(x => x.YValues[0]).Min();
double maxx = allPoints.Select(x => x.XValue + ((SizeF)x.Tag).Width).Max();
double maxy = allPoints.Select(x => x.YValues[0] + ((SizeF)x.Tag).Height).Max();
ca.AxisX.Minimum = minx;
ca.AxisX.Maximum = maxx;
ca.AxisY.Minimum = miny;
ca.AxisY.Maximum = maxy;
}
I want to print all item's Barcode using PrintPreviewDialog. Written code works fine but the problem is on ending first page i called e.HasMorePages = true which creates 2nd page but not printing 2nd page. it means 2nd page got empty /blank. As you can see in below pic, at last line Barcode got cropped automatically and not print remaining Barcodes on 2nd page. Kindly help how to print remaining Barcode on 2nd page.
as you can see in above pic, the last line got cropped automatically and 2nd page not printing and got empty page.
PrintPreviewDialog code;
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintBarcodeEvent_PrintPage);
System.Windows.Forms.PrintDialog pdd = new System.Windows.Forms.PrintDialog();
pdd.Document = pd;
System.Windows.Forms.DialogResult result = pdd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
System.Windows.Forms.PrintPreviewDialog pp = new System.Windows.Forms.PrintPreviewDialog();
pp.Document = pd;
result = pp.ShowDialog();
//pd.Print();
}
Event:
// The PrintPage event is raised for each page to be printed.
private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
{
int startX = 5;
int startY = 5;
Database db = new Database();
db.DBOpen();
int NBbarcode_perLine = 5;
int numbarcode = 0;
int barcodePerPage = 35;
int countBarcodePerPage = 0;
for (int i = 0; i < listTobePrint.Count; i++)
{
String code = listTobePrint[i].Code;
String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);
Font printFont = new Font("Arial", 10.0f);
e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
startX, startY, new StringFormat());
int x2 = startX + 3;
int y2 = startY + 15;
e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);
int x3 = startX;
int y3 = y2 + 50;
e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
x3, y3, new StringFormat());
int x4 = startX;
int y4 = y3 + 15;
e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
x4, y4, new StringFormat());
int x5 = startX;
int y5 = y4 + 15;
e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
x5, y5, new StringFormat());
numbarcode++;
countBarcodePerPage++;
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
startX = 5;
startY += 150; // space between 2 barcode in vertical (upper left). you have to adjust)
numbarcode = 0;
}
if (countBarcodePerPage >= barcodePerPage)
{
//MessageBox.Show(countBarcodePerPage.ToString());
e.HasMorePages = true;
//startX = 5;
//startY = 5;
} else
{
e.HasMorePages = false;
}
}
db.DBClose();
listTobePrint.Clear();
}
you have to create a global variable countbarcode (not local to PrintPage) or static and initialise To zéro.
Each time you set e.HasMorePages to true, the event PrintPage is called
Its not a problem for a test, but i think you should put the db.open in event BeginPrint and db.close in event EndPrint . the database is open and close each time a page is printed. (maybe its not really important, but you avoid to recreate the same variable db and waste meory)
private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
{
int startX = 5;
int startY = 5;
Database db = new Database();
db.DBOpen();
int NBbarcode_perLine = 5;
int numbarcode = 0;
int barcodePerPage = 35;
int countBarcodePerPage = 0;
int totalcodebar = listTobePrint.Count;
for (int i = 0; i < barcodePerPage; i++)
{
String code = listTobePrint[countbarcode].Code;
String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);
Font printFont = new Font("Arial", 10.0f);
e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
startX, startY, new StringFormat());
int x2 = startX + 3;
int y2 = startY + 15;
e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);
int x3 = startX;
int y3 = y2 + 50;
e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
x3, y3, new StringFormat());
int x4 = startX;
int y4 = y3 + 15;
e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
x4, y4, new StringFormat());
int x5 = startX;
int y5 = y4 + 15;
e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
x5, y5, new StringFormat());
numbarcode++;
countbarcode++;
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
startX = 5;
startY += 150; // space between 2 barcode in vertical (upper left). you have to adjust)
numbarcode = 0;
}
if (countbarcode == totalcodebar) break;
if (i == barcodePerPage - 1)
{
db.DBClose();
e.HasMorePages = true;
return;
}
}
e.HasMorePages = false;
db.DBClose();
listTobePrint.Clear();
}
I want to print 50 labels per sheet (5 columns and 10 rows). When it goes to the 11th row it should switch to the next page. I have tried e.hasMorePages in several ways, but sometimes it gets overlapped on the same page.
Here is my code:
private void MakingLabel(int curentIndex,List<BarcodesSpecs> List)
{
int ListRows = List.Count;
BarcodesSpecs barcodesSpecs = List.ElementAt(curentIndex);
BarCode_ItemCode = barcodesSpecs.ItemCodeMain;
BarCode_Description = barcodesSpecs.Description;
BarCode_SalePrice = barcodesSpecs.SalePrice;
BarCode_Size = barcodesSpecs.Size;
BarCode_Colour = barcodesSpecs.Colour;
barCode_LabelToPrint = Convert.ToInt16(barcodesSpecs.QtyToPrint);
}
int xCord = 0, yCord = 0;
int CurentIndex = 0;
int MaxCharactersInString = 26;
private void printBarcodes_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
for (CurentIndex = 0; CurentIndex < BcList.Count; CurentIndex++)
{
MakingLabel(CurentIndex, BcList);
for (int i = 0; i < barCode_LabelToPrint; i++) //// making Copies means How many Labels Of this Item Needed
{
if (xCord >= 750)
{
xCord = 0;
yCord += 115;
}
if (BarCode_Description.Length > MaxCharactersInString)
{
BarCode_Description = BarCode_Description.Substring(0, MaxCharactersInString);
}
e.Graphics.DrawString("ALPIAL SUITING", new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new Point(xCord, yCord + 10));
e.Graphics.DrawString("Rs" + BarCode_SalePrice + "/-", new Font("Arial", 14, FontStyle.Bold), Brushes.Black, new Point(xCord, yCord + 21));
e.Graphics.DrawString("Size: " + BarCode_Size + " Colour: " + BarCode_Colour, new Font("Arial", 07, FontStyle.Regular), Brushes.Black, new Point(xCord, yCord + 42));
e.Graphics.DrawString(BarCode_Description, new Font("Arial", 07, FontStyle.Bold), Brushes.Black, new Point(xCord, yCord + 52));
e.Graphics.DrawString(BarCode_ItemCode, new Font("Arial", 06, FontStyle.Bold), Brushes.Black, new Point(xCord, yCord + 62));
Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
e.Graphics.DrawImage(barcode.Draw(BarCode_ItemCode, 25), xCord, yCord + 72);
xCord += 160;
}
}
}
The result I am getting is in the picture, any help will be appreciated thanks!
Somewhere along the line, you will need to check if yCord is beyond the bottom of the page. If it is, you need to set HasMorePages to true and exit the PrintPage handler. It will be called again for the next page. You'll have to keep track of which labels you have already printed outside the PrintPage handler and continue from that point.
Here's a sample I did to simulate labels printing. I only drew a square to represent the label. I had to do a little math to figure out the spacing so you will likely have to adjust this for your situation.
private static void doPrintPreview()
{
var pd = new PrintDocument();
pd.PrintPage += pd_PrintPage;
var prv = new PrintPreviewDialog();
prv.Document = pd;
prv.ShowDialog();
}
//Units are in 1/100 of an inch
private static float leftMargin = 100f; //Page margins
private static float rightMargin = 750f;
private static float topMargin = 100f;
private static float bottomMargin = 1000f;
private static int numLabelsToPrint = 200; //How many we want to print
private static int numLabelsPrinted = 0; //How many we have already printed
private static float labelSizeX = 75; //Label size
private static float labelSizeY = 75f;
private static float labelGutterX = 7.14f; //Space between labels
private static float labelGutterY = 7.5f;
static void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Display; //Units are 1/100 of an inch
//start at the left and top margin of the page for a new page
float xPos = leftMargin;
float yPos = topMargin;
using (var p2 = new Pen(Brushes.Red, 3.0f))
{
//While there are still labels to print
while (numLabelsPrinted < numLabelsToPrint)
{
//Draw the label (i just drew a square)
e.Graphics.DrawRectangle(Pens.Red, xPos, yPos, labelSizeX, labelSizeY);
numLabelsPrinted++;
//Set the x position for the next label
xPos += (labelSizeX + labelGutterX);
//If the label will be printed beyond the right margin
if ((xPos + labelSizeX) > rightMargin)
{
//Reset the x position back to the left margin
xPos = leftMargin;
//Set the y position for the next row of labels
yPos += (labelSizeY + labelGutterY);
//If the label will be printed beyond the bottom margin
if ((yPos + labelSizeY) > bottomMargin)
{
//Reset the y position back to the top margin
yPos = topMargin;
//If we still have labels to print
if (numLabelsPrinted < numLabelsToPrint)
{
//Tell the print engine we have more labels and then exit.
e.HasMorePages = true;
//Notice after setting HasMorePages to true, we need to exit from the method.
//The print engine will call the PrintPage method again so we can continue
//printing on the next page.
break; //you could also just use return here
}
}
}
}
}
}
I want to draw a street with a panel but it wont work. I want to have it with a loop but i cant get it done. The wall and roof must become 20% smaller than the house on the left.
My code:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
HuisTekenen();
}
}
private void HuisTekenen()
{
gebouw();
dak();
}
private void gebouw()
{
Graphics paper;
paper = panel1.CreateGraphics();
Pen pennetje = new Pen(Color.Green);
int b = 100;
int aantal = 0;
for (int i = 10; aantal <= 5; i += 120)
{
paper.DrawRectangle(pennetje, i, 100, b, 150);
aantal++;
i = i / 100 * 80;
b = b / 100 * 80;
}
}
private void dak()
{
Graphics paper;
paper = panel1.CreateGraphics();
Pen pennetje = new Pen(Color.Red);
int b = 100;
int aantal = 0;
for (int i = 10; aantal <= 5; i+=120)
{
paper.DrawLine(pennetje, i, 10 / 100 * 80, i, b);
paper.DrawLine(pennetje, i, 10 / 100 * 80, i + 100, b);
aantal++;
i = i / 100 * 80;
b = 100 / 100 * 80;
}
}
The result i want to get:
Result i get:
Can you help me?
Thanks!
Here's a way to get started with the box. Youll have to finish your homework on your own.
float size = 50;
float xpos = 0;
float ypos = 0;
for(int i=0;i<5;i++) //whatever you do, this must be what your loop looks like. anything else is going way off in the wrong direction
{
paper.DrawRectangle(pennetje, xpos, ypos, size, size);
xpos += size + 20;
size *= .8f;
}
First a remark: your for-statements are odd, usually the three parts will use the same variable. It can work this way, but doing it like this is certainly not recommended.
A different kind of issue will be this statement:
i = i / 100 * 80;
With variable i being an int, and / performing integer division, the first part i / 100 will produce 0 because that is the int value closest to the real/float result. And of course multiplying 0 by 80 will not do anything.
I suggest you now try again :-)
I'm writing an extended progress bar control at present, 100% open source and I've created some basic styles with gradients and solid colours.
One of the options I wanted to add was animation to the bar, prety much like the windows 7 and vista green progress bar. So I need to add a moving "Glow" to the % bar, however my attempt at this looks terrible.
My method is to draw an ellipse with set size and move it's x position until it reaches the end were the animation starts again.
First of does anyone have nay links or code to help me achieve the current windows 7 glow effect using GDI or some similar method?
I have several other animations that will also be added the the bar hence the GDI.
private void renderAnimation(PaintEventArgs e)
{
if (this.AnimType == animoptions.Halo)
{
Rectangle rec = e.ClipRectangle;
Rectangle glow = new Rectangle();
//SolidBrush brush = new SolidBrush(Color.FromArgb(100, Color.White));
//int offset = (int)(rec.Width * ((double)Value / Maximum)) - 4;
int offset = (int)(rec.Width / Maximum) * Value;
if (this.animxoffset > offset)
{
this.animxoffset = 0;
}
glow.Height = rec.Height - 4;
if (this.animxoffset + glow.X > offset)
{
glow.Width = offset - (this.animxoffset + 50);
}
else
{
glow.Width = 50;
}
glow.X = this.animxoffset;
LinearGradientBrush brush = new LinearGradientBrush(glow, Color.FromArgb(0, Color.White), Color.FromArgb(100, Color.White), LinearGradientMode.Horizontal);
e.Graphics.FillEllipse(brush, this.animxoffset, 2, glow.Width, glow.Height);
brush.Dispose();
string temp = offset.ToString();
e.Graphics.DrawString(temp + " : " + glow.X.ToString(), DefaultFont, Brushes.Black, 2, 2);
animTimer = new System.Timers.Timer();
animTimer.Interval = 10;
animTimer.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
animTimer.Start();
}
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.animTimer.Stop();
this.animxoffset += 2;
Invalidate();
}
This is just an example glow iterating through a pen array.
You could also use a transparent image (although it could have a performance impact).
Pen[] gradient = { new Pen(Color.FromArgb(255, 200, 200, 255)), new Pen(Color.FromArgb(150, 200, 200, 255)), new Pen(Color.FromArgb(100, 200, 200, 255)) };
int x = 20;
int y = 20;
int sizex = 200;
int sizey = 10;
int value = 25;
//draw progress bar basic outline (position - 1 to compensate for the outline)
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x-1, y-1, sizex, sizey));
//draw the percentage done
e.Graphics.FillRectangle(Brushes.AliceBlue, new Rectangle(x, y, (sizex/100)*value, sizey));
//to add the glow effect just add lines around the area you want to glow.
for (int i = 0; i < gradient.Length; i++)
{
e.Graphics.DrawRectangle(gradient[i], new Rectangle(x - (i + 1), y - (i + 1), (sizex / 100) * value + (2 * i), sizey + (2 * i)));
}