Solved

form custom shape from png

Posted on 2006-11-12
6
256 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

912 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

20 Experts available now in Live!

Get 1:1 Help Now