Solved

form custom shape from png

Posted on 2006-11-12
6
257 Views
Last Modified: 2010-08-05
hi,
vb2005

im trying to have a form have a custom shape from a .png image. is this possible?
thanks
0
Comment
Question by:newyuppie
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17925693
Yes you can, I know you like links :) so :

http://www.freevbcode.com/ShowCode.asp?ID=6146

In the example it uses an simpson.png (not included !). But I just ran with an image on my machine with a mostly white background.
It skinned to the image nicely.
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17925764
hi kinger, congrats on the 50000 points.

code does indeed work and it skins nicely. only problem i see is that its a bit on the slow side. for instance, im using a form set as a splashscreen, and using this code, it shows *after* the main form loads, so its taking a while to draw. on another sad note, it flickers for 1 second before drawing and its very noticeable.
does that happen in your computer too?

NY
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 500 total points
ID: 17925982
Thanks, didn't notice but I'm very pleased.

Ok, the problem is bad. But there is a solution. The process is looping through all items in the image matrix, so thats the slow part.
So what I would do, it run the code again with the new Form1_Load code below
What this does is on the first run, it creates a text file c:\test.txt with the coordinates from the image.
Then it loads these coordinates from the text file into a rectangles array and uses that to create the region.
So save these coordinates in a local file (included in your app). And call these coordinates instead.

The below code includes writing the coordinates to a texfile and retrieving them. Obviously you will only need to do this once to get the data in the text file, then copy it to an internal project file and use that in future runs to create the form region.


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'If setting the background image at design-time, remove the next 2 lines:
        Dim Background As New Bitmap("c:\piccy.bmp")
        Me.BackgroundImage = Background
        Me.Width = Me.BackgroundImage.Width
        Me.Height = Me.BackgroundImage.Height
        Me.FormBorderStyle = FormBorderStyle.None

        '//-- Create a rectangles array from the forms region
        Dim rec() As RectangleF = MakeRegion(Me.BackgroundImage).GetRegionScans(New Drawing2D.Matrix)

        '//-- Save to text file.
        Dim strRecCoord As String
        Dim objStreamWriter As System.IO.StreamWriter
        objStreamWriter = New System.IO.StreamWriter("C:\Test.txt")

        For i As Integer = 0 To rec.GetUpperBound(0) - 1
            strRecCoord = rec(i).X & ":" & rec(i).Y & ":" & rec(i).Width & ":" & rec(i).Height & "|"
            objStreamWriter.Write(strRecCoord)
        Next

        objStreamWriter.Close()

        '//-- Read from text file.
        Dim objStreamReader As New System.IO.StreamReader("C:\Test.txt")
        Dim Rect() As String = objStreamReader.ReadToEnd.Split("|")
        objStreamReader.Close()

        '//-- Split for rect
        Dim RectRegion(Rect.GetUpperBound(0)) As RectangleF
        For i As Integer = 0 To Rect.GetUpperBound(0) - 1
            RectRegion(i).X = Rect(i).Split(":")(0)
            RectRegion(i).Y = Rect(i).Split(":")(1)
            RectRegion(i).Width = Rect(i).Split(":")(2)
            RectRegion(i).Height = Rect(i).Split(":")(3)
        Next

        '//-- Set the region
        Dim gp As New System.Drawing.Drawing2D.GraphicsPath
        gp.AddRectangles(RectRegion)
        Me.Region = New Region(gp)
        gp.Dispose()
    End Sub
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 13

Author Comment

by:newyuppie
ID: 17926806
very savvy indeed!
it does seem to work fine, let me perform some tests ill get back to you
NY
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17955935
sorry to have taken some days, i was off. great answer!
NY
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17956087
no problem, yes thought I hadn't seen any comments from you for a couple of days ..
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now