none
g.DrawImage and Distortion

    Question

  • Hola,

     

    I'm a VB beginner.  I have some code below to distort an image (bmpR), to which gradient brush is to be applied.

     

    Dim rect9 As New Rectangle(0, Source.Height, Source.Width + xWidth, xHeight)
    Dim myLinearGradientBrush0 As New LinearGradientBrush(rect0, Color.Transparent, xColor, LinearGradientMode.Vertical)
    Dim destinationPoints0 As Point() = {New Point(0, Source.Height), New Point(Source.Size.Width, Source.Height), New Point(xWidth, Source.Height + xHeight)}
    g.DrawImage(bmpR, destinationPoints0)
    g.FillRectangle(myLinearGradientBrush0, rect9)


    Actually, the code says that image(bmpR) is distorted at first.  Then gradient brush is applied.  Next, I have the following amended code.

     

    Dim rect9 As New Rectangle(0, Source.Height, Source.Width + xWidth, xHeight)
    Dim myLinearGradientBrush0 As New LinearGradientBrush(rect0, Color.Transparent, xColor, LinearGradientMode.Vertical)
    Dim destinationPoints0 As Point() = {New Point(0, Source.Height), New Point(Source.Size.Width, Source.Height), New Point(xWidth, Source.Height + xHeight)}
    g.DrawImage(bmpR, 0, Source.Height, Source.Width, xHeight) 'Not yet distorted...
    g.FillRectangle(myLinearGradientBrush0, rect9)


    Now, gradient brush is applied to bmpR at first.  I don't know how to distort bmpR with destinationPoints0.  Which graphic method do I use to transform it?

     

    Muchas gracias,

     

    Onion Ring (fried)


    Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
    Wednesday, November 30, 2011 5:10 AM

All replies

  • Hi,

    do you mean something like: (Note that this will only shear the image, if you want to distort like warping, you'll have to displace and interpolate the pixels (-array) of the image itself)

     

    Imports System.Drawing.Drawing2D
    
    Public Class Form1
        Dim WithEvents pictureBox1 As New PictureBox()
        Dim WithEvents pictureBox2 As New PictureBox()
        Dim WithEvents button1 As New Button()
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.ClientSize = New Size(800, 500)
            Me.pictureBox1.Location = New Point(0, 0)
            Me.pictureBox1.ClientSize = New Size(400, 400)
            Me.Controls.Add(pictureBox1)
    
            Me.pictureBox2.Location = New Point(400, 0)
            Me.pictureBox2.ClientSize = New Size(400, 400)
            Me.Controls.Add(pictureBox2)
    
            button1.Text = "distort"
            button1.Location = New Point(400, 410)
            Me.Controls.Add(button1)
    
            Dim bmp As New Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height)
    
            Using g As Graphics = Graphics.FromImage(bmp)
                g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                g.FillRectangle(Brushes.Green, New Rectangle(0, 0, bmp.Width, bmp.Height))
                g.FillEllipse(Brushes.Red, New Rectangle(0, 0, bmp.Width, bmp.Height))
    
                Using p As New Pen(Color.Yellow, 12)
                    g.DrawRectangle(p, New Rectangle(0, 0, bmp.Width, bmp.Height))
                End Using
            End Using
    
            Me.pictureBox1.Image = bmp
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            Dim widthAdd As Integer = 200
            Dim rect9 As New Rectangle(0, 0, pictureBox1.Image.Width + widthAdd, pictureBox1.Image.Height)
            Dim b As Bitmap = New Bitmap(rect9.Width, rect9.Height)
    
            Using g As Graphics = Graphics.FromImage(b)
                Using myLinearGradientBrush0 As New LinearGradientBrush(rect9, Color.Transparent, Color.Maroon, LinearGradientMode.Vertical)
                    'upleft, upright, lowerLeft
                    Dim destinationPoints0 As Point() =
                    {
                        New Point(0, 0),
                        New Point(pictureBox1.Image.Width, 0),
                        New Point(widthAdd, pictureBox1.Image.Height)
                    }
                    g.DrawImage(Me.pictureBox1.Image, destinationPoints0)
    
                    'clockwise
                    Dim destinationPoints1 As Point() =
                    {
                        New Point(0, 0),
                        New Point(pictureBox1.Image.Width, 0),
                        New Point(rect9.Width, rect9.Height),
                        New Point(widthAdd, pictureBox1.Image.Height)
                    }
                    g.FillPolygon(myLinearGradientBrush0, destinationPoints1)
                End Using
            End Using
    
            Dim img As Image = pictureBox2.Image
    
            Me.pictureBox2.Image = b
    
            If Not img Is Nothing Then
                img.Dispose()
            End If
    
            Me.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom
        End Sub
    
        Private Sub Me_Closing(sender As Object, e As EventArgs) Handles Me.FormClosing
            If Not pictureBox1.Image Is Nothing Then
                pictureBox1.Image.Dispose()
            End If
    
            If Not pictureBox2.Image Is Nothing Then
                pictureBox2.Image.Dispose()
            End If
        End Sub
    End Class
    
    

    Regards,

      Thorsten


    Wednesday, November 30, 2011 11:45 AM
  • Thanks.  I don't see how your code can help me, though.
    Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
    Wednesday, November 30, 2011 2:35 PM
  • Hi,

    I think, I understood now... you only want to draw the bmpR (where everything else is already drawn) in that sheared Parallelogram? Is that correct?

    Wednesday, November 30, 2011 2:48 PM
  • ... if so, create a new Bitmap of desired size and do draw out the bmpR to the new Bitmap with the destination0-points array...

     

            'your code to edit bmpR
            '...
            'adjust with and height to fit your needs
            Dim b As New Bitmap(Source.Width + xWidth, Source.Height + xHeight)
    
            Using gx As Graphics = Graphics.FromImage(b)
                gx.DrawImage(Source, destinationPoints0)
            End Using
    
            'now use this image
            Me.pictureBox1.Image = b
    

     


    Wednesday, November 30, 2011 2:55 PM
  • >I think, I understood now... you only want to draw the bmpR (where everything else is already drawn) in that sheared Parallelogram? Is that correct?

     

    Yes.

     

    I've done something similar, creating a new bitmap.  With the code you've posted above, the image (bmpR) won't be distorted.

     

    Thanks.


    Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
    Wednesday, November 30, 2011 3:48 PM
  • I give up.  Thank you for your help.
    Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
    Wednesday, November 30, 2011 4:08 PM
  • if destinationPoints0 defines a Parallelogram thats sized different as the original-image-rectangle, the code will shear the image.

    orig,

    code from reply1, code from reply2:

     

    Wednesday, November 30, 2011 4:13 PM