View Single Post
 
Old 07-13-2014, 06:36 PM
Rodrigo Mota Rodrigo Mota is offline
Regular
 
Join Date: Apr 2005
Location: Portugal
Posts: 53
Default Function returning image

Hi guys, I'm stuck in this for quite sometime and I don't even know if what I want is possible.
Sorry about the title it doesn't describe exactly what I pretend but I didn't know how to put it.
So here it goes, I've drawn a shape with GDI and I want to be able to put it in a function in order to get it whenever I want and pass the parameters.

Here's the code:

Code:
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        'VÁLVULA 40 X 40
        Dim t1 As New Point(0, 10)
        Dim t2 As New Point(0, 30)
        Dim t3 As New Point(20, 20)
        Dim t4 As New Point(40, 10)
        Dim t5 As New Point(40, 30)
        Dim t6 As New Point(20, 10)
        Dim r2 As Rectangle = New Rectangle(10, 0, 20, 20)

        Dim aIni As Single = 0
        Dim aAmp As Single = -180
        Dim l As Pen = New Pen(Brushes.Black, 2)

        Dim vT1 As Point() = {t1, t2, t3}
        Dim vT2 As Point() = {t3, t4, t5}

        Dim vP As Bitmap = New Bitmap(40, 40)
        Dim vPeq As Graphics = Me.CreateGraphics
        vPeq = Graphics.FromImage(vP)
        With vPeq
            .FillPolygon(Brushes.Yellow, vT1)
            .DrawPolygon(l, vT1)
            .FillPolygon(Brushes.Yellow, vT2)
            .DrawPolygon(l, vT2)
            .FillPie(Brushes.Yellow, r2, aIni, aAmp)
            .DrawPie(l, r2, aIni, aAmp)
            .DrawLine(l, t3, t6)
        End With

        e.Graphics.DrawImage(vP, 50, 100)
    End Sub

End Class
This part works fine, what I want to do is having a function with the drawing part and be able to call from the sub.
I'll post what I tried to do with no success:

Code:
  Function valve(vt1() As Point, vt2() As Point, aIni As Single, anAmp As Single, l1 As Point, l2 As Point, r As Rectangle) As Graphics
        Dim l As Pen = New Pen(Brushes.Black, 2)
        With valve
            .FillPolygon(Brushes.Yellow, vt1)
            .DrawPolygon(l, vt1)
            .FillPolygon(Brushes.Yellow, vt2)
            .DrawPolygon(l, vt2)
            .FillPie(Brushes.Yellow, r, aIni, aAmp)
            .DrawPie(l, r, aIni, aAmp)
            .DrawLine(l, l1, l2)
        End With
        Return valve
    End Function
Is there a way of doing a function like this so that when I draw the image, I don't have to write the code every time.
I'm still taking my 1st steps in .net so I'm struggling a bit with all the concepts around it.

I hope I explained myself clearly and looking forward for some help.
Thank you very much for your time and patience.
Reply With Quote