C# rotate a pdf upon creation - c#
i'm working on method that create a delivery order pdf but recently i have been asked to add more columns in the array. As a result my array doesn't fit in Portrait size anymore so i'm trying to rotate my page in Landscape mode but i can't manage to make it work here is my code (some word have been modified/deleted for professional secret but it remain understandable. Thanks in advance
public static void CreateDocument(String to)
{
System.Collections.Generic.List<Produit> listPro = new System.Collections.Generic.List<Produit>(MainWindow.produits);
//listPro = ;
// Create a new PDF document
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
document.Info.Title = "Bon de livraison n°"+MainWindow.numOrder;
int numpage = 1;
decimal maxPerPage = 25.00M;
Decimal numberPage = listPro.Count / maxPerPage;
Console.WriteLine("nbr : "+ listPro.Count+ "/ 25 = "+ numberPage);
numberPage = Math.Ceiling(numberPage);
String watermark = "NE PAS DIFFUSER";
while (1 == 1)
{
// Create an empty page
PdfSharp.Pdf.PdfPage page = document.AddPage();
page.Orientation = PageOrientation.Landscape;
page.Rotate = (page.Rotate - 90) % 360;
// Get an XGraphics object for drawing watermark
var gfx2 = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
XFont fontWater = new XFont("Verdana", 40, XFontStyle.Bold);
XSize size = gfx2.MeasureString(watermark, fontWater);
// Define a rotation transformation at the center of the page
gfx2.TranslateTransform(page.Width / 2, page.Height / 2);
gfx2.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx2.TranslateTransform(-page.Width / 2, -page.Height / 2);
// Create a string format
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
// Create a dimmed red brush
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
// Draw the string
gfx2.DrawString(watermark, fontWater, brush,
new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
format);
gfx2.Dispose();
// Get an XGraphics object for drawing the order
XGraphics gfx = XGraphics.FromPdfPage(page);
/*gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) *180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);*/
// Create a font
XFont font = new XFont("Verdana", 11, XFontStyle.Bold);
XFont fontRegular = new XFont("Verdana", 8, XFontStyle.Regular);
XFont fontMedium = new XFont("Verdana", 7, XFontStyle.Regular);
XFont fontLittle = new XFont("Verdana", 6, XFontStyle.Regular);
XFont fontRegularBold = new XFont("Verdana", 8, XFontStyle.Bold);
// Draw the text
gfx.DrawString("Bon de livraison", font, XBrushes.Black,
new XRect(0, 0, page.Width, 100),
XStringFormats.Center);
gfx.DrawString(to, font, XBrushes.Black,
new XRect(0, 0, page.Width, 130),
XStringFormats.Center);
gfx.DrawString("Produit à usage non thérapeutique", fontRegular, XBrushes.Black,
new XRect(0, 0, page.Width, 160),
XStringFormats.Center);
int x, y;
x = 40;
y = 80;
if (numpage == 1)
{
// gfx.DrawString(to, font, XBrushes.Black, page.Width, 100);
gfx.DrawString("Producteur : ", fontRegular, XBrushes.Black, x - 20, y - 50);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 10, y - 40);
gfx.DrawString(" ", fontRegular, XBrushes.Black, x - 10, y - 30);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 10, y - 20);
gfx.DrawString("N°BL", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString(MainWindow.numOrder.ToString(), fontRegular, XBrushes.Black, x + 113, y);
x = 700;
gfx.DrawString("to : ", fontRegular, XBrushes.Black, x - 30, y - 50);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 40);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 30);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 20);
gfx.DrawString("Technicien", fontRegular, XBrushes.Black, x - 20, y);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 120, y - 10);
gfx.DrawLine(XPens.Black, x + 60, y + 4, x + 120, y + 4);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 60, y + 4);
gfx.DrawLine(XPens.Black, x + 120, y - 10, x + 120, y + 4);
gfx.DrawString(MainWindow.intervenant, fontRegular, XBrushes.Black, x + 63, y);
x = 40;
y = y + 20;
gfx.DrawString("Date d'envoi des Produits", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString("10/03/2020", fontRegular, XBrushes.Black, x + 113, y);
x = 700;
gfx.DrawString("Nombre de produit", fontRegular, XBrushes.Black, x - 20, y);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 120, y - 10);
gfx.DrawLine(XPens.Black, x + 60, y + 4, x + 120, y + 4);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 60, y + 4);
gfx.DrawLine(XPens.Black, x + 120, y - 10, x + 120, y + 4);
gfx.DrawString(listPro.Count + "", fontRegular, XBrushes.Black, x + 63, y);
x = 40;
y = y + 20;
gfx.DrawString("Date d'envoi du BL", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString("10/03/2020", fontRegular, XBrushes.Black, x + 113, y);
}
else
{
y = 100;
}
x = 50;
y += 20;
int ydebut = y;
int xdebut = x;
if (to == "Yes")
{
gfx.DrawString("", fontRegular, XBrushes.Black, x -= 38, y + 12);
gfx.DrawString("Type", fontRegular, XBrushes.Black, x += 60, y + 12);
gfx.DrawString("N°Lymphobank", fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString("Volume",fontRegular, XBrushes.Black, x+=40,y+12);
/*gfx.DrawString("IH et groupe sanguin", fontRegular, XBrushes.Black, x += 80, y + 12);
gfx.DrawString("Age", fontRegular, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("Sexe", fontLittle, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Résultats Qualification Biologique du Don", fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Blocage", fontLittle, XBrushes.Black, x += 370, y + 12);
gfx.DrawString("NbrDon", fontLittle, XBrushes.Black, x += 30, y + 12);
gfx.DrawString("Donneur Connu", fontRegular, XBrushes.Black, x += 30, y + 12);
*/// gfx.DrawLine(XPens.Black, xdebut - 40, y, x, y);
}
if (to != "YES")
{
gfx.DrawString("Numéro Lymphobank", fontMedium, XBrushes.Black, x += 3, y + 12);
gfx.DrawString("Volume (ml)", fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("IH et groupe sanguin", fontMedium, XBrushes.Black, x += 53, y + 12);
gfx.DrawString("Age", fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("Sexe", fontMedium, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Résultats Qualification Biologique du Don", fontMedium, XBrushes.Black, x += 25, y + 12);
gfx.DrawString("Blocage", fontMedium, XBrushes.Black, x += 370, y + 12);
gfx.DrawString("Nbr Don", fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawString("Donneur Connu", fontMedium, XBrushes.Black, x += 40, y + 12);
}
int Nproduit = 0;
foreach (Produit pro in listPro)
{
Nproduit++;
y += 15;
x = xdebut;
// NumeroYES = prod.GetYES()
//NumeroTypeProduit = prod.GetNumTypeYESProduit()
// Sexe = "" + prod.GetSexe() + "",
//DateNaissance = "" + prod.GetDateNaissance() + "",
if (to == "YES")
{
//gfx.DrawLine(XPens.Black, x -= 40, y + 3, xdebut, y + 3);
gfx.DrawString(pro.GetYES(), fontRegular, XBrushes.Black, x += 3, y + 12);
gfx.DrawString(pro.GetNumTypeYESProduit(), fontRegular, XBrushes.Black, x += 60, y + 12);
gfx.DrawString(pro.ToString(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString(pro.GetVolume(), fontRegular, XBrushes.Black, x += 30, y + 12);
/* gfx.DrawString(pro.GetGroupeSimple(pro.GetGroupeProduit()) + pro.GetGroupeNonElargie(pro.GetGroupeProduit()), fontRegular, XBrushes.Black, x += 80, y + 12);
gfx.DrawString(pro.GetDateNaissance(), fontRegular, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("" + pro.GetSexe() + "", fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetResultatQBDTrans(), fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetBlocage(), fontRegular, XBrushes.Black, x += 370, y + 12);
gfx.DrawString(pro.GetNombreDon(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString(pro.GetDonneurConnu(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x += 80 - 3, y + 3);*/
}
else
{
gfx.DrawString(pro.ToString(), fontMedium, XBrushes.Black, x += 3, y + 12);
gfx.DrawString(pro.GetVolume(), fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString(pro.GetGroupeSimple(pro.GetGroupeProduit()) + pro.GetGroupeNonElargie(pro.GetGroupeProduit()), fontMedium, XBrushes.Black, x += 53, y + 12);
gfx.DrawString(pro.GetDateNaissance(), fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("" + pro.GetSexe() + "", fontMedium, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetResultatQBDTrans(), fontMedium, XBrushes.Black, x += 25, y + 12);
gfx.DrawString(pro.GetBlocage(), fontMedium, XBrushes.Black, x += 370, y + 12);
gfx.DrawString(pro.GetNombreDon(), fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawString(pro.GetDonneurConnu(), fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x += 90 - 3, y + 3);
}
if (Nproduit == 25)
{
break;
}
}
listPro.RemoveRange(0, Nproduit);
y += 15;
x = xdebut;
if (to == "YES")
{
/*gfx.DrawLine(XPens.Black, x - 40, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, x -= 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 60, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 80, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 370, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 80, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut - 40, ydebut, x, ydebut);*/
}
else
{
/* gfx.DrawLine(XPens.Black, x, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 25, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 370, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, ydebut, x, ydebut);
*/
}
gfx.DrawString("Page "+numpage + "/" + numberPage, fontRegular, XBrushes.Black, 750, 580);
if (listPro.Count > 0)
{
Console.WriteLine("Nombre "+listPro.Count);
numpage++;
}
else
{
break;
}
}
// Save the document...
string filename = "BonDeLivraison_" + to+".pdf";
try
{
document.Save(filename);
Console.WriteLine("Fichier sauvegarder : "+filename);
//OUverture du document pur debug a enelever (mettre en commentaire)
//Process.Start(filename);
//decommenter pour que ca imprime automatiquement
RawPrinterHelper.SendFileToPrinter(MainWindow.imprimanteBon.getImprimanteText(),filename);
}
catch(Exception err)
{
MessageBox.Show(err+"", "Suppression du produit");
}
}
Related
Calculating Next 10 in Value
I have this code that adds a dot per every 10 pixels (x), and I need to calculate the next 10 pixels per every count from 'Dots' int. Graphics g = e.Graphics; int Dots = 6; g.FillEllipse(b, new Rectangle(x, y, 5, 5)); g.FillEllipse(b, new Rectangle(x + 10, y, 5, 5)); g.FillEllipse(b, new Rectangle(x + 20, y, 5, 5)); if (Dots > 3) { for (int i = Dots - 1; i >= 0; i--) { // How to iterate next 10 based on the number of values > 3 ? g.FillEllipse(b, new Rectangle(x, y, 5, 5)); } } I need for to iterate each of the follow lines (as a example) and allow it to bump up 10 for every value > 5 found in Dots. Example - x + 20, y, 5, 5 x + 30, y, 5, 5 x + 40, y, 5, 5 x + 50, y, 5, 5 etc.. I hope it makes sense. Thank you
SOLVED for (int i = Dots - 1; i >= 0; i--) { g.FillEllipse(b, new Rectangle(x + (i * 10), y, 5, 5)); } I learned in order to step 10 every iteration, use this in place of the expected value: (i * 10)
Print data from several lists to several pages through PrintPage in C#
I have to print the data and results of my application through the PrintPage event. The problem is that I have the data and results to print on List elements, my first group of List is printed correctly through a While cycle; but when I add another While to print another group, this starts printing on the same pages printed above, not over writing the pages, but, which starts the next printing on the margins left in the first. Annex fragment of my code. void PrintDocument1PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { owner = (MainForm)this.Owner; pcombo = owner.stadoCarga.pcombo; printDocument1.PrintController.IsPreview.ToString(); printDocument1.DefaultPageSettings.Margins.Left = 60; printDocument1.DefaultPageSettings.Margins.Right = 60; printDocument1.DefaultPageSettings.Margins.Top = 240; printDocument1.DefaultPageSettings.Margins.Bottom = 60; Font fte = new Font("verdana", 11, FontStyle.Regular); Font ftti = new Font("verdana", 11, FontStyle.Bold); Font ft1 = new Font("verdana", 10, FontStyle.Regular); Font Fgr = new Font("Impact", 22); SolidBrush br = new SolidBrush(Color.Black); Single xPos = e.MarginBounds.Left; Single yPos = 220; //e.MarginBounds.Top; Single hight = e.MarginBounds.Height; Single incLine = 25; int printHeight; printHeight = e.MarginBounds.Height - printDocument1.DefaultPageSettings.Margins.Bottom - printDocument1.DefaultPageSettings.Margins.Top; int r = 0; frmDiagr MSCh = new frmDiagr(); frmInfo picLogo = new frmInfo(); LineasPagina = Convert.ToInt32(printHeight / fte.GetHeight(e.Graphics)); e.Graphics.DrawImage(picLogo.picLogo.Image, 60, 50); e.Graphics.DrawString("ADIMA v2.0 Pro", Fgr, Brushes.Green, 230, 50); e.Graphics.DrawString("Análisis y Diseño de Muros de Mampostería", ftti, Brushes.Green, 230, 87); e.Graphics.DrawString("Proyecto.:", ft1, Brushes.Black, 60, 120); e.Graphics.DrawString(owner.datos.DatosInfo[0], ft1, Brushes.Black, 140, 120); e.Graphics.DrawString("Cliente.:", ft1, Brushes.Black, 510, 120); e.Graphics.DrawString(owner.datos.DatosInfo[1], ft1, Brushes.Black, 575, 120); e.Graphics.DrawString("Ingeniero.:", ft1, Brushes.Black, 60, 140); e.Graphics.DrawString(owner.datos.DatosInfo[2], ft1, Brushes.Black, 140, 140); e.Graphics.DrawString("Codigo.:", ft1, Brushes.Black, 510, 140); e.Graphics.DrawString(owner.datos.DatosInfo[4], ft1, Brushes.Black, 575, 140); // e.Graphics.DrawString("___________________________________________________________________", fte, Brushes.Black, 50, 180) e.Graphics.DrawString("DATOS PARA EL DISEÑO", ftti, Brushes.Black, 60, 170); e.Graphics.DrawString("- REFUERZOS UNIFORME DE LA SECCION DE MURO", ftti, Brushes.Black, 295, 170); e.Graphics.DrawString("MURO No.", fte, Brushes.Black, 60, 210); e.Graphics.DrawString("LONG.(m)", fte, Brushes.Black, 170, 210); e.Graphics.DrawString("ESP.(m)", fte, Brushes.Black, 270, 210); e.Graphics.DrawString("ALT(m)", fte, Brushes.Black, 360, 210); e.Graphics.DrawString("f'm(kg/c2)", fte, Brushes.Black, 440, 210); e.Graphics.DrawString("Em(kg/c2)", fte, Brushes.Black, 540, 210); e.Graphics.DrawString("Ev(kg/c2)", fte, Brushes.Black, 640, 210); e.Graphics.DrawString("PV(to/m3)", fte, Brushes.Black, 740, 210); int stop = contador + LineasPagina;// Contador mas cantidad de lineas por pagina if(stop > ak.Count){ stop = ak.Count; } while (contador < stop) { int k = Convert.ToInt32(ak[contador]); r = Convert.ToInt32(ak[contador])+1; e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine); e.Graphics.DrawString(muroToDiseno[k].ToString(), fte, Brushes.Black, 180, yPos + incLine); e.Graphics.DrawString(owner.datosDibujo.espMuro[k].ToString(), fte, Brushes.Black, 280, yPos + incLine); e.Graphics.DrawString(owner.datos.Datos[3], fte, Brushes.Black, 370, yPos + incLine); e.Graphics.DrawString(owner.datos.Datos[7], fte, Brushes.Black, 460, yPos + incLine); e.Graphics.DrawString(owner.datos.Datos[8], fte, Brushes.Black, 550, yPos + incLine); e.Graphics.DrawString(txtEsfEv.Text, fte, Brushes.Black, 650, yPos + incLine); e.Graphics.DrawString(owner.datos.Datos[12], fte, Brushes.Black, 750, yPos + incLine); incLine += 25; contador++; } PaginaNumero += 1; e.HasMorePages = contador < ak.Count; // Hasta Aqui se Imprime Correctamente.... int stopC = count + LineasPagina;// Contador mas cantidad de lineas por pagina if(stopC > ak.Count){ stopC = ak.Count; } for (int i = 0; i < sumaFactorizada.Count; i++) { while(count < stopC){ int k = Convert.ToInt32(ak[count]); r = Convert.ToInt32(ak[count])+1; e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine); e.Graphics.DrawString(Math.Round(sumaFactorizada[i][k],3).ToString(), fte, Brushes.Black, 180, yPos + incLine); e.Graphics.DrawString(EQFactorizada[i][k].ToString(), fte, Brushes.Black, 260, yPos + incLine); incLine += 25; count++; } PaginaNumero += 1; e.HasMorePages = count < ak.Count; } }
How can i ressize my rectangles using mouse events in c#?
I've been drawn multiple rectangles on the picturebox image.I want to resize all that rectangles using mouse events. Can anyone help me out regarding this? public List<rectangle> listRec = new List<rectangle>(); Graphics g; //private Graphics g; Point startPos; Point currentPos; bool drawing; Rectangle r1; Rectangle rect = new Rectangle(); private Rectangle getRectangle() { r1 = new Rectangle( Math.Min(startPos.X, currentPos.X), Math.Min(startPos.Y, currentPos.Y), Math.Abs(startPos.X - currentPos.X), Math.Abs(startPos.Y - currentPos.Y)); return r1; } private void button1_Click(object sender, EventArgs e) { String data; Font font = new Font("Arial", 14); arg1 = Convert.ToInt32(textBox1.Text); arg2 = Convert.ToInt32(textBox2.Text); Rectangle rect = new Rectangle(); rect.Size = new Size(40, 65); for (int x = 0; x < arg1; x++) { // rect.X = x * rect.Width; rect.X = x * (rect.Width + 30) + 73; for (int y = 0; y < arg2; y++) { rect.Y = y * (rect.Height + 35) + 38; listRec.Add(rect); data = rect.ToString(); TextWriter txt = new StreamWriter("E:\\B1Pockets.txt", true); txt.WriteLine(data); txt.Close(); // MessageBox.Show(rect.ToString()); } } foreach (Rectangle rec in listRec) { g = pictureBox1.CreateGraphics(); Pen p = new Pen(Color.Red, 3); g.DrawRectangle(p, rec); g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 30), 35); g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 35); g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, 35); g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 30), (rect.Height + 30) + 40); g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, (rect.Height + 30) + 40); g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, (rect.Height + 30) + 40); } } I've tried this code to my application.I drawn rectangles in 2*3 manner.and also draw a big rectangle above it.In short my picturebox containing many rectangles and i want to add resizing options for all the rectangles in c#
Joining bezier curve to other drawings
I have implemented a rounded rectangle extension method, defined here. public static Graphics DrawRRectangle(this Graphics g, Pen p, int x, int y, int width, int height, int feathering) { g.DrawLine(p, x, y + feathering, x, y + height - feathering); g.DrawBezier(p, new Point(x, y + height - feathering), new Point(x, y + height - feathering / 2), new Point(x + feathering / 2, y + height), new Point(x + feathering, y + height)); g.DrawLine(p, x + feathering, y + height , x + width - feathering, y + height); g.DrawBezier(p, new Point(x + width - feathering, y + height), new Point(x + width - feathering / 2, y + height), new Point(x + width, y + height - feathering / 2), new Point(x + width, y + height - feathering)); g.DrawLine(p, x + width, y + height - feathering, x + width, y + feathering); g.DrawBezier(p, new Point(x + width, y + feathering), new Point(x + width, y + feathering / 2), new Point(x + width - feathering / 2, y), new Point(x + width - feathering, y)); g.DrawLine(p, x + width - feathering, y, x + feathering, y); g.DrawBezier(p, new Point(x + feathering, y), new Point(x + feathering / 2, y), new Point(x, y + feathering / 2), new Point(x, y + feathering)); return g; } However when I use this method like so g.DrawRRectangle(p, 100, 100, 1000, 1000, 100);, I do not get the outcome I wanted, each of the corners are either misaligned of their pixels do not match up As seen in the images below. Any suggestions anybody could offer would be helpful, I am unsure if this is a problem with the equations used to generate my curves however this is the first time I am dabbling with graphics, so it could just be my thinking. Thanks.
Whilst I can't comment on your implementation, you're going to run into problems further down the road with this. Your implementation will give the appearance of drawing a rounded rectangle, but say for example in future you want to fill the shape, you won't be able to because GDI/GDI+ won't see the drawn shapes as a single consecutive shape. In this respect you should use a GraphicsPath. See here for a complete solution for drawing rounded rectangles using a GraphicsPath.
Converting C++ code to C# code
I'm trying to convert the C++ opencv code demo drawContour functionality to C#. I'm having trouble with function arguments contours0[k] and contours[k] in function API Cv2.ApproxPolyDP. I get Design time compiler error stating some of the parameters are invalid for Cv2.ApproxPolyDP call. The code in question is listed below. Thanks in advance for your assistances. using System; using System.Collections.Generic; using System.Windows.Forms; using OpenCvSharp; class Program { const int w = 500; static int levels = 3; static Point[][] contours; static HierarchyIndex[] hierarchy; static void on_trackbar(int pos, object UseData) { Mat cnt_img = Mat.Zeros(w, w, MatType.CV_8UC3); int _levels = levels - 3; Cv2.DrawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar.White, 3, LineTypes.AntiAlias, hierarchy, Math.Abs(_levels) ); Cv2.ImShow("contours", cnt_img); } static void Main() { Mat img = Mat.Zeros(w, w, MatType.CV_8UC1); //Draw 6 faces for( int i = 0; i < 6; i++ ) { int dx = (i % 2) * 250 - 30; int dy = (i/2)*150; Scalar white = Scalar.White; Scalar black = Scalar.Black; if( i == 0 ) { for( int j = 0; j <= 10; j++ ) { double angle = (j + 5) * Math.PI/21; Cv2.Line(img, new Point(Math.Round(dx + 100 + j * 10 - 80 * Math.Cos(angle), 0), Math.Round(dy + 100 - 90 * Math.Sin(angle), 0)), new Point(Math.Round(dx + 100 + j * 10 - 30 * Math.Cos(angle), 0), Math.Round(dy + 100 - 30 * Math.Sin(angle), 0)), white, 1); } } Cv2.Ellipse(img, new Point(dx + 150, dy + 100), new Size(100, 70), 0, 0, 360, white); Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size( 30, 20), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size( 30, 20), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size( 15, 15), 0, 0, 360, white); Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size( 15, 15), 0, 0, 360, white); Cv2.Ellipse(img, new Point(dx + 115, dy + 70), new Size( 5, 5), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 185, dy + 70), new Size( 5, 5), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 150, dy + 100), new Size( 10, 5), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 150, dy + 150), new Size( 40, 10), 0, 0, 360, black); Cv2.Ellipse(img, new Point(dx + 27, dy + 100), new Size( 20, 35), 0, 0, 360, white); Cv2.Ellipse(img, new Point(dx + 273, dy + 100), new Size( 20, 35), 0, 0, 360, white); } //show the faces Cv2.ImShow( "image", img ); //Extract the contours so that Point[][] contours0; Cv2.FindContours( img, out contours0, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple); contours = new Point[contours0.Length][]; for( int k = 0; k < contours0.Length; k++ ) Cv2.ApproxPolyDP(contours0[k], contours[k], 3, true); // compiler error! CvTrackbar Track = new CvTrackbar("levels+3", "contours", 3, 7, on_trackbar); on_trackbar(0, 0); Cv2.WaitKey(); } }
I needed to rewrite the C++ line: approxPolyDP(Mat(contours0[k]), contours[k], 3, true); as C# line: contours[k] = Cv2.ApproxPolyDP(contours0[k], 3, true);