picturebox image text print is not clear - c#

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Font font = new Font("Arial", 8, FontStyle.Regular);
//e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
// e.SmoothingMode = SmoothingMode.AntiAlias;
// e.InterpolationMode = InterpolationMode.HighQualityBicubic;
//e.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.DrawString("FIRST NAME" , myFont, Brushes.Black, 15, 90);
e.Graphics.DrawString("MIDDLE NAME", font, Brushes.Black, 15, 105);
e.Graphics.DrawString("LASTNAME", font, Brushes.Black, 15, 120);
e.Graphics.DrawString("ADDRESS", font, Brushes.Black, 15, 135);
e.Graphics.Flush();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float x = 0;
float y = 0;
//SolidBrush myBrush = new SolidBrush(Color.Black);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
print font word are not clear visible when i print picturebox image.
i want clear font (like microsoft word or excel print) letters visible when i printing images.
means good pixels
anybody suggest me?

Related

Printing Multiple Pages C#

I'm trying to print some text data using an event handler, and then print a PDF on the second page.
I'm using pdfium viwer to do this and it works individually. But I'm struggling to combine them.
I need to get the two to print as 2 pages of the same print document, as I want to use a duplex printer to print the pdf on the back. I don't just want to send two individual pages.
Sample code:
To Print PDF:
private void button1_Click(object sender, EventArgs e)
{
string filena = #textBox1.Text;
PrintPDF("M2020", "A4", filena, 1);
}
public bool PrintPDF(string printer, string paperName, string filename, int copies)
{
try
{
// Create the printer settings for our printer
var printerSettings = new PrinterSettings
{
PrinterName = printer,
Copies = (short)copies,
};
// Create our page settings for the paper size selected
var pageSettings = new PageSettings(printerSettings)
{
Margins = new Margins(0, 0, 0, 0),
};
foreach (PaperSize paperSize in printerSettings.PaperSizes)
{
if (paperSize.PaperName == paperName)
{
pageSettings.PaperSize = paperSize;
break;
}
}
// Now print the PDF document
using (var document = PdfDocument.Load(filename))
{
using (var printDocument = document.CreatePrintDocument())
{
printDocument.PrinterSettings = printerSettings;
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}
return true;
}
catch
{
return false;
}
}
And my original code to print a QR code:
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
string s2 = "Modified by: ";
string s6 = "Author: " + author;
string s4 = "Created: " + drawdate;
QRCodeGenerator qrGenerator1 = new QRCodeGenerator();
QRCodeData qrCodeDataPrint = qrGenerator1.CreateQrCode(qrcodedata, QRCodeGenerator.ECCLevel.Q, false, false);
Bitmap qrCodeImagePrint = (new QRCode(qrCodeDataPrint)).GetGraphic(20);
Bitmap bmimg = new Bitmap(this.pictureBox2.Width, this.pictureBox2.Height);
this.pictureBox2.DrawToBitmap(bmimg, new Rectangle(0, 0, this.pictureBox2.Width, this.pictureBox2.Height));
System.Drawing.Font f1 = new System.Drawing.Font("Arial", 5f, FontStyle.Bold, GraphicsUnit.Millimeter);
System.Drawing.Font f2 = new System.Drawing.Font("Arial", 2f, GraphicsUnit.Millimeter);
System.Drawing.Font f3 = new System.Drawing.Font("Arial", 3f, GraphicsUnit.Millimeter);
System.Drawing.Font barc = new System.Drawing.Font("Code39Azalea", 36f, GraphicsUnit.Point);
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
Pen blackPen = new Pen(Color.Black, 1f);
Rectangle rect = new Rectangle(165, 35, 30, 30);
Rectangle qrcodetest = new Rectangle(170, 40, 25, 25);
Rectangle btmimg = new Rectangle(60, 140, 120, 120);
Rectangle combox = new Rectangle(20, 145, 160, 40);
e.Graphics.DrawImage(qrCodeImagePrint, qrcodetest);
e.Graphics.DrawRectangle(blackPen, qrcodetest);
e.Graphics.DrawString(this.procreq, f1, Brushes.Black, new Point(50, 5));
e.Graphics.DrawString(s1, f1, Brushes.Black, new Point(150, 70));
e.Graphics.DrawString(s1z, f2, Brushes.Black, new Point(150, 75));
e.Graphics.DrawString(qrissuedby, f2, Brushes.Black, new Point(20, 264));
e.Graphics.DrawString(s6, f2, Brushes.Black, new Point(20, 267));
e.Graphics.DrawString(s4, f2, Brushes.Black, new Point(20, 270));
e.Graphics.DrawString(s2, f2, Brushes.Black, new Point(20, 273));
e.Graphics.DrawString(s3, f2, Brushes.Black, new Point(20, 276));
e.Graphics.DrawString(s5, f2, Brushes.Black, new Point(20, 279));
e.Graphics.DrawString(this.notebox.Text, f3, Brushes.Black, combox);
e.Graphics.DrawImage(bitmap1, btmimg);
bmimg.Dispose();
bitmap1.Dispose();
}
Any help would be much appreciated!
Thanks
Andrew
Ok, think i was going about that in completely the wrong way. What I eventually did was use the pdfium viewer to render the pdf as a bitmap, and then printed that as the second page.
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
Pen blackPen = new Pen(Color.Black, 1f);
int actualwidth = (int)(e.Graphics.DpiX * 210 / 25.4f);
int actualHeight = (int)(e.Graphics.DpiY * 297 / 25.4f);
using (var pdfDocument = PdfiumViewer.PdfDocument.Load(#textBox1.Text))
{
var bitmapImage = pdfDocument.Render(0, actualwidth, actualHeight, false);
pictureBox1.Image = bitmapImage;
pictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipNone));
}
if (pgnum == 1)
{
Debug.WriteLine("firing first page " + pgnum);
Rectangle qrcodetest = new Rectangle(170, 40, 25, 25);
e.Graphics.DrawRectangle(blackPen, qrcodetest);
e.HasMorePages = true;
pgnum++;
Debug.WriteLine(pgnum);
}
else
{
Debug.WriteLine("firing second page " + pgnum);
GraphicsUnit units = GraphicsUnit.Millimeter;
Rectangle srcRect = new Rectangle(0, 0, 199, 281);
//e.Graphics.Q
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.DrawImage(pictureBox1.Image, srcRect);
e.HasMorePages = false;
}
This does work.... but the printed version of the PDF is very poor quality. I've tried using interpolation etc.... but doesn't seem to make any difference, so not sure i'm putting it in the right place?
Also, i've tried using the bitmap variable rather than the picturebox image, but it seems to generate the same result. The PDF prints much better.
Thanks
Andrew

How to draw text with transparency value with C#?

I create a from with transparent background color and I want to draw "hello" string on the form window with a proper transparency setting:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(150, 50);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.CompositingMode = CompositingMode.SourceOver;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
g.TextContrast = 10;
Font font = new Font("", 20, FontStyle.Bold);
Color color = ColorTranslator.FromHtml("#D3D3D3");
int opacity = 180;
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, color));
g.DrawString("hello", font, brush, 10, 10);
g.Save();
g.Dispose();
bmp.MakeTransparent(Color.Transparent);
e.Graphics.DrawImage(bmp, 0, 0);
bmp.Dispose();
}
When I set opacity to 1 or 2, the image text disappears, when set to 3, display dark black color, when set to 254, a little transparent. Anything wrong in my code?
I update code to:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Black);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.CompositingMode = CompositingMode.SourceOver;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
g.TextContrast = 3;
Font font = new Font("", 20, FontStyle.Bold);
Color color = ColorTranslator.FromHtml("#191970");
int opacity = 20;
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, color));
g.DrawString("hello", font, brush, 20, 20);
bmp.MakeTransparent(Color.Black);
g.Save();
g.Dispose();
e.Graphics.DrawImage(bmp, 0, 0);
bmp.Dispose();
}
Output on screen:

Print Text with DrawString() next to an existing bitmap

Greetings fellow users,
A virgin post on my end since its the first time i am abusing stack overflow with a question! I have been trying to get a bitmap print along with a String to print. Basically the view i want to achieve is the Image and the text to the right of the image as we see the printout. Below is the code I am using
Bitmap qrCodeImage = qrCode.GetGraphic(20);
senderQR = qrCodeImage;
PrintDocument pd = new PrintDocument();
Margins margins = new Margins(10, 10, 10, 10);
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += PrintPage;
pd.Print();
Here is the PrintPage method
private void PrintPage(object sender, PrintPageEventArgs e)
{
System.Drawing.Image img = senderQR;
Bitmap batchCode = new Bitmap(80, 700);
Rectangle m = e.MarginBounds;
RectangleF batch1 = new RectangleF(80, 700, 650, 1000);
m.Width = img.Width / 5;
m.Height = img.Height / 5;
Graphics g = Graphics.FromImage(batchCode);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawString(batch, new Font("Arial", 40), Brushes.Black, batch1);
g.Flush();
e.Graphics.DrawImage(img, m);
}
What am i doing wrong? what seems to be the issue? I have been struggling a whole lot to achieve this but no luck!
Additional Notes:
I want the text on the right to Wrap under itself and not under or on top of the existing bitmap within a size of 3,5 x 2 (inches) (label printing).
This is the printout i get with the existing code;
https://prnt.sc/h1ecb0
https://prnt.sc/h1edex
The image you're drawing on (batchCode) is 80 pixels wide and 700 high. When you write your text over it, you set the top-left point of your writing to 80,700 - exactly to the bottom-right corner of your picture. Basically, you write your text outside of the picture.
Update
I've created a small example to make it reproducible, below is a form class for a basic WinForms application:
public partial class Form1 : Form
{
private PictureBox pictureBox2;
public Form1()
{
InitializeComponent();
pictureBox2 = new PictureBox();
pictureBox2.Size = ClientSize;
pictureBox2.SizeMode = PictureBoxSizeMode.AutoSize;
this.Click += Form1_Click;
pictureBox2.Click += Form1_Click;
Controls.Add(pictureBox2);
}
private void Form1_Click(object sender, EventArgs e)
{
var batch = "hello there!";
Bitmap batchCode = new Bitmap(1000, 1000);
var batch1 = new RectangleF(150, 150, 850, 850);
using (Graphics g = Graphics.FromImage(batchCode))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawString(batch, new Font("Arial", 40), Brushes.Black, batch1);
}
pictureBox2.Image = batchCode;
}
}

Draw on picture box and print it

Hi I want to make my picture box to be a circle-shaped
Then print it.
The problem is I can see in the form that the picture box is circle but when I'm previewing it to print it's not circle.
Here's my code
public Form1()
{
InitializeComponent();
//This makes picturebox1 circle
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, pictureBox1.Width - 4, pictureBox1.Height - 4);
Region rg = new Region(gp);
pictureBox1.Region = rg;
}
//Preview the print
private void button1_Click(object sender, EventArgs e)
{
printPrev.Document = printDoc;
printPrev.ShowDialog();
}
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Draw the picturebox on PDF
e.Graphics.DrawImage(pictureBox1.Image, 230, 230);
}
Thanks
it is not working because you are not changing the Image.
You are only changing the graphics.
You could do something like this.
Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawEllipse(new Pen(new SolidBrush(Color.Black),3),0,0,bitmap.Width -4,bitmap.Height - 4);
pictureBox1.Image = bitmap;
This will also solve your problem
using (var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height)) {
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width,bmp.Height));
e.Graphics.DrawImage(bmp, 230, 230);
}

visual c# - onPaint and transparency

I am making a simple form with two semi-transparent texts
and i put it in a paint event.
only, when I wider the form, the texts turn darker and grainy.
actualy I want the darker color but not the grainy effect.
here is my code snippet:
private void sbfToolBox_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
string drawString = "tekst";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 50);
Color color_red = Color.FromArgb(30, 100, 0, 0);
Color color_cyan = Color.FromArgb(30, 0, 100, 100);
System.Drawing.SolidBrush brush_red = new System.Drawing.SolidBrush(color_red);
System.Drawing.SolidBrush brush_cyan = new System.Drawing.SolidBrush(color_cyan);
float x = 0.0F;
float x2 = 20.0F;
float y = 50.0F;
formGraphics.DrawString(drawString, drawFont, brush_red, x, y);
formGraphics.DrawString(drawString, drawFont, brush_cyan, x2, y);
drawFont.Dispose();
brush_red.Dispose();
brush_cyan.Dispose();
formGraphics.Dispose();
}
thanks in advance
Use the Graphics object from PaintEventArgs.
Change
System.Drawing.Graphics formGraphics = this.CreateGraphics();
To
System.Drawing.Graphics formGraphics = e.Graphics;
And remove
formGraphics.Dispose();

Categories