vb.net export pdf Font not displaying correctly - vb.net

i'm trying to export with Unicode font text but output not displaying correctly
enter image description here
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pdfDoc As New PdfDocument()
Dim pdfPage As PdfPage = pdfDoc.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(pdfPage)
Dim font As New XFont("Noto Sans Sinhala", 12, XFontStyle.Regular)
gfx.DrawString("අද හරි ලස්සන දවසක් ගමට යමි", font, XBrushes.Black, New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormat.Center)
pdfDoc.Save("SinhalaText.pdf")
End Sub
End Class

Related

VB.NET Winforms: Overlay two images and move overlay with timer

I would like to overlay 2 images and then move the top layer using a timer.
this is the code i'm trying to implement from this anwser: here
Imports System.Drawing
Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)
g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage
If you want to have the timer move the overlayed image, then first, make a variable
Dim posX As Integer = 0
then use
g.DrawImage(OverlayImage, posX, 0)
Now when your timer ticks, increment posX by 10
This is what i got but the overlay isn't moving.
What am i doing wrong? Can anybody help me?
Imports System.Drawing
Public Class Form1
Dim OverlayImage As New Bitmap("D:/white.png", True)
Dim BackImage As New Bitmap("D:/bg.png", True)
Dim g As Graphics = Graphics.FromImage(BackImage)
Dim posX As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Picturebox1.Image = BackImage
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
g.DrawImage(OverlayImage, posX, 0)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
posX = +10
End Sub
End Class

Moving a bitmap to the front

So
I need to put an image on a picturebox
I have a form with a panel which contain a picturebox and an image.
I use image because I need to rotate it.
what I get is that the picture cover the image and I need the opposite.
picturebox.sendToBack doesnt work and i cant find img.bringToTop function.
Imports System.Drawing.Drawing2D
Public Class Form1
Dim g As Graphics
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
g = Panel1.CreateGraphics
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As Image
img = Bitmap.FromFile(Application.StartupPath & "/pic/" & "clay.jpg")
g.RotateTransform(330)
g.DrawImage(img, 70, 170, 100, 100)
Pb.SendToBack()
End Sub
End Class
[ pictures][1]
https://i.stack.imgur.com/s9goC.jpg
Here is an example of what I linked to in the comments that applies to your specific scenario. This code will draw the same Image on a Panel and a PictureBox. It will work whether the PictureBox is a child of the Panel or a sibling.
Private ReadOnly img As Image = Image.FromFile("file path here")
Private Sub DrawImage(c As Control, g As Graphics)
'The image will be drawn at (100,100) in form coordinates.
'Translate that location to coordinates for the current control.
Dim imgLocation = c.PointToClient(Me.PointToScreen(New Point(100, 100)))
g.DrawImage(img, imgLocation)
End Sub
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
DrawImage(Panel1, e.Graphics)
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
DrawImage(PictureBox1, e.Graphics)
End Sub
In that code, there is an individual Paint event handler for each control and they both call a common method that does the drawing. If you want to, you can bundle it all into a single common event handler:
Private ReadOnly img As Image = Image.FromFile("file path here")
Private Sub Controls_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint,
PictureBox1.Paint
'The image will be drawn at (100,100) in form coordinates.
'Translate that location to coordinates for the current control.
Dim imgLocation = DirectCast(sender, Control).PointToClient(Me.PointToScreen(New Point(100, 100)))
e.Graphics.DrawImage(img, imgLocation)
End Sub

How can I print a document with textboxes showing? (vB)

So I have been trying to print a document where the textboxes are shown on top of a picturebox, however it just doesn't seem to work.
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width / 0.95 - mPrintBitMap.Width) \ 1
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height / 0.9 - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
picFij.SendToBack()
lblDN.BringToFront()
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
mPrintDocument = New PrintDocument
printPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
I attempted a BringToFront() and SendToBack() but that didn't work.
This is what I want to print:
https://cdn.discordapp.com/attachments/358502382910570497/546555282940100648/unknown.png
And this is print preview
https://cdn.discordapp.com/attachments/358502382910570497/546555621806178324/unknown.png
Any ideas?
Make the PictureBox the Parent of your TextBox, then it should show up when you call DrawToBitmap(). For example, to keep TextBox1 in the same location, convert it screen coords, then back to client coords with respect to the PictureBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pt As Point = TextBox1.PointToScreen(New Point(0, 0))
TextBox1.Parent = PictureBox1
TextBox1.Location = PictureBox1.PointToClient(pt)
End Sub

VB.NET Class method handles paint event

How can i use clause Handles in my class method. For example i want to draw an image on picturebox1 with code:
Public Class cell
Public Sub draw_cell() Handles picturebox1.paint
code
End Sub
End Class
I have an error:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
How can i do this without using
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
ps. sorry for bad english.
You can create your own routines to draw to your canvas.
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim canvasGraphics As Graphics = Graphics.FromImage(canvas)
Dim cellRect As New Rectangle(100, 100, 100, 100)
cell.draw(canvasGraphics, cellRect, Color.Red, New Pen(New SolidBrush(Color.Green), 1))
PictureBox1.Image = canvas
End Sub
Public Class cell
Public Shared Sub draw(ByRef canvasGraphics As Graphics, ByVal cellRect As Rectangle, ByVal fillColor As Color, ByVal borderPen As Pen)
Dim renderedCell As New Bitmap(cellRect.Width, cellRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim borderRect As New Rectangle(0, 0, cellRect.Width - 1, cellRect.Height - 1)
Dim context As BufferedGraphicsContext
Dim bGfx As BufferedGraphics
context = BufferedGraphicsManager.Current
context.MaximumBuffer = New Size(cellRect.Width + 1, cellRect.Height + 1)
bGfx = context.Allocate(Graphics.FromImage(renderedCell), New Rectangle(0, 0, cellRect.Width, cellRect.Height))
Dim g As Graphics = bGfx.Graphics
g.Clear(fillColor)
g.DrawRectangle(borderPen, borderRect)
bGfx.Render()
canvasGraphics.DrawImage(renderedCell, cellRect.Location)
End Sub
End Class
End Class

Draw graphics on screen

I am using Visual Basic.Net and am drawing graphics on the screen.
Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim gr As Graphics = Graphics.FromHwnd(New IntPtr(0))
gr.DrawString("text on screen",
New Font(Me.Font.FontFamily, 25,
FontStyle.Regular), Brushes.Red, 50, 50)
End Sub
In the above code, text is drawn on the screen. My question is this: How can I remove the text that is drawn to screen? I see that there is a .Clear method, however, this 'Clears the entire drawing surface and fills it with the specified background color', rather than just removing the drawn text.
Thanks in advance.
EDIT
I am wanting to develop a subliminal message application that will show messages on screen while the user is using other applications. Would the transparent form be the best way to do this?
I have found the following code that works:
Private WithEvents TextForm As New Form
Private Zipper As New FontFamily("Zipper")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With TextForm
.BackColor = Color.DimGray
.TransparencyKey = Color.DimGray
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.ShowInTaskbar = False
.WindowState = FormWindowState.Maximized
.Opacity = 0
.Show(Me)
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static showText As Boolean
showText = Not showText
If showText Then TextForm.Opacity = 0.99 Else TextForm.Opacity = 0
End Sub
Private Sub TextForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles TextForm.Paint
e.Graphics.DrawString("text on screen 12345", New Font(Zipper, 30, FontStyle.Bold), Brushes.Red, 50, 50)
End Sub
Take different bitmaps and draw each new thing in separate bitmaps and after merge new bitmap with old one. When you want to remove text reload old bitmap which is without text.
Search for drawing in new bitmaps and save drawing.
You can try this:
Dim Graphics0 as Graphics = Graphics.fromHwnd(0) 'This is the desktop's graphics
Graphics0.DrawText("Test 1..2..3..",New Font(Arial,10),Brushes.Black,New Point(0,0))