Creating a Image out of multiple Controls on a User Control

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi I am using transparent controls(transparent due to their back end code on one user control). All the inner controls (which are apparently labels) host png/jpg images (note transparency involved here).


Now everything works on the USer Control while I add images(labels with Image). However when I try to save this to a png/bmp I get stupid images. I am not sure where I am going wronng. Please help. I post both Screenshot and Saved Image.

<pre class="prettyprint lang-vb Public Sub GetBMP(ByRef bmpfinal As Bitmap)
Dim noChildren = Me.Controls.Count
Dim bmpinternal As New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
DeselectAll()
Me.DrawToBitmap(bmpinternal, Me.ClientRectangle)
If noChildren = 0 Then Exit Sub
Dim g As Graphics = Graphics.FromImage(bmpinternal)
For i As Integer = noChildren - 1 To 0 Step -1
For Each c1 As Control In Me.Controls
If i = Me.Controls.GetChildIndex(c1) Then
Dim nr As New Bitmap(c1.Width, c1.Height)
Dim rect As New Rectangle
rect.Location = New Point(IIf(c1.Left < 0, 0, c1.Left), IIf(c1.Top < 0, 0, c1.Top))
rect.Size = New Size(IIf(c1.Right > Me.ClientRectangle.Width, Me.ClientRectangle.Width - rect.Left, c1.Right - rect.Left), IIf(c1.Bottom > Me.ClientRectangle.Height, Me.ClientRectangle.Height - rect.Top, c1.Bottom - rect.Top))
c1.DrawToBitmap(nr, New Rectangle(IIf(c1.Left < 0, Math.Abs(c1.Left), 0), IIf(c1.Top < 0, Math.Abs(c1.Top), 0), c1.Width, c1.Height))
Dim crpimage As New Bitmap(rect.Width, rect.Height)
Dim g2 As Graphics = Graphics.FromImage(crpimage)
g2.DrawImageUnscaled(nr, IIf(c1.Left < 0, Math.Abs(c1.Left), 0), IIf(c1.Top < 0, Math.Abs(c1.Top), 0), rect.Width, rect.Height)
g.DrawImage(crpimage, rect)
crpimage.Dispose()
nr.Dispose()
End If
Next
Next
Dim g1 As Graphics = Graphics.FromImage(bmpfinal)

g1.DrawImage(bmpinternal, Me.Location)
bmpinternal.Dispose()
End Sub[/code]
Actual Screen with Controls
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/161441
Saved Image due to above Code
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/161442
Thanks in Advance
Vijay S

View the full article
 
Back
Top