Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to Fill Image into Round Corner Rectangle... I researched internet Found some codes and Modified. I get what I want in PictureBoX FillPath.

Only diffences is ;
in PictureBox

Dim grpX As Graphics = PictureBox1.CreateGraphics 


and in PrintDocument

Dim grpX As Graphics = e.Graphics 


other all Codes are same. But in PictureBox İmage fit Perfectly. otherside in PrintDocument image only fill a place as Rectangle size.

How can I fill Rectangle in PrintDocument as Filled in PictureBox ?

What I have tried:

VB
Public Function createRoundRect(ByVal R As Rectangle, ByVal radius As Integer) As GraphicsPath
        Dim gp As New GraphicsPath()
        Dim x As Integer, y As Integer, w As Integer, h As Integer
        x = R.X
        y = R.Y
        w = R.Width
        h = R.Height
        If radius = 0 Then
            gp.AddRectangle(New Rectangle(x, y, w, h))
        Else
            gp.AddLine(x + radius, y, x + w - radius, y)
            gp.AddArc(x + w - radius, y, radius, radius, 270, 90)
            gp.AddLine(x + w, y + radius, x + w, y + h - radius)
            gp.AddArc(x + w - radius, y + h - radius, radius, radius, 0, 90)
            gp.AddLine(x + w - radius, y + h, x + radius, y + h)
            gp.AddArc(x, y + h - radius, radius, radius, 90, 90)
            gp.AddLine(x, y + h - radius, x, y + radius)
            gp.AddArc(x, y, radius, radius, 180, 90)
            gp.CloseFigure()
        End If
        Return gp
    End Function

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim newImage As Image = Drawing.Image.FromFile("C:\Users\Yaman\Desktop\Frames\384.jpg")
        Dim imaj As Bitmap = New Bitmap("C:\Users\Yaman\Desktop\Frames\384.jpg")
        Dim grpX As Graphics = PictureBox1.CreateGraphics 

        Dim rectX As Rectangle = New Rectangle(CDec(10), CDec(10), CDec(200), CDec(100))
        Dim tg As Graphics = Graphics.FromImage(imaj) 
        tg.SmoothingMode = SmoothingMode.AntiAlias
        grpX.SmoothingMode = SmoothingMode.AntiAlias
        tg.DrawImage(newImage, CDec(10), CDec(10), CDec(200), CDec(100))
        Dim TBrush As New TextureBrush(imaj)
        Dim gp As New GraphicsPath
        gp = createRoundRect(rectX, 12)
        grpX.FillPath(TBrush, gp)

        End Sub

    Private Sub PrintDocument2_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument2.PrintPage

        Dim newImage As Image = Drawing.Image.FromFile("C:\Users\Yaman\Desktop\Frames\384.jpg")
        Dim imaj As Bitmap = New Bitmap("C:\Users\Yaman\Desktop\Frames\384.jpg")
        Dim grpX As Graphics = e.Graphics 

        Dim rectX As Rectangle = New Rectangle(CDec(10), CDec(10), CDec(200), CDec(100))
        Dim tg As Graphics = Graphics.FromImage(imaj) 
        tg.SmoothingMode = SmoothingMode.AntiAlias
        grpX.SmoothingMode = SmoothingMode.AntiAlias
        tg.DrawImage(newImage, CDec(10), CDec(10), CDec(200), CDec(100))        
 Dim TBrush As New TextureBrush(imaj)
        Dim gp As New GraphicsPath
        gp = createRoundRect(rectX, 12)
        grpX.FillPath(TBrush, gp)

    End Sub
Posted
Updated 16-May-20 15:07pm
v3
Comments
CHill60 18-May-20 4:20am    
Please don't delete the contents of your post like this. If you want to delete the post then use the "dustbin" icon that will appear when you hover a mouse over your question

1 solution

What on earth is this garbage all over the place?
CDec(10)

Don't do that. You can specify a Decimal constant in VB.NET by appending a D to the value: 10D 200D

Your code doesn't take into account the behavior of the PictureBox control that don't exist in a printed copy, nor does it take into account the much higher resolution of a printer over your monitor. If your image is 200x200 on the screen, it's going to be tiny on a printer. Monitors today are typcially 96dpi or higher where printers can be as high as 2400dpi.

You need to scale your drawing code values up to account for the differences.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900