Dear Sir,
I'm trying to export png from the panel in the form to a transparent png file in VB.NET.
if I use code option 1 it works because the background image of the panel is already a transparent png while I use code option 2 it does not work and in my panel, there is an additional small image with a picture box.
Please guide me
Thanks
Code option 1
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim folderPath As String = Application.StartupPath()
Private SmallImage As Byte()
Private Sub ExportPanelToPNG()
' Create a bitmap with the same size as the panel
Dim bitmap As New Bitmap(Panel1.Width, Panel1.Height, PixelFormat.Format32bppArgb)
Dim bitmap As New Bitmap(Panel1.Width, Panel1.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.AntiAlias
g.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim savePath As String = folderPath & "\ExportedPanel.png"
Dim ResizedImage As Image = ResizeImage(Panel1.BackgroundImage, 600, 600)
SmallImage = SaveImage(ResizedImage)
ResizedImage.Save(savePath, ImageFormat.Png)
bitmap.Dispose()
Process.Start(savePath)
End Using
End Sub
Private Function ResizeImage(ByRef InputImage As Image, MaxWidth As Integer, MaxHeight As Integer) As Image
'ReSize the Image if needed to save space in the database
If MaxWidth < InputImage.Width Or MaxHeight < InputImage.Height Then
'ReSize Image
Dim Scale As Double = Math.Min(MaxWidth / InputImage.Width, MaxHeight / InputImage.Height)
Return New Bitmap(InputImage, New Size(Math.Round(InputImage.Width * Scale), Math.Round(InputImage.Height * Scale)))
Else
'Image size was OK
Return InputImage
End If
End Function
Private Function SaveImage(TheImage As Image) As Byte()
'Converts Image to Byte Array
Using mStream As New MemoryStream()
'TheImage.Save(mStream, TheImage.RawFormat)
TheImage.Save(mStream, ImageFormat.Png)
Return mStream.ToArray()
End Using
End Function
End Class
Code option 2
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim folderPath As String = Application.StartupPath()
Private SmallImage As Byte()
Private Sub ExportPanelToPNG()
' Create a bitmap with the same size as the panel
Dim bitmap As New Bitmap(Panel1.Width, Panel1.Height, PixelFormat.Format32bppArgb)
Dim bitmap As New Bitmap(Panel1.Width, Panel1.Height, PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.AntiAlias
g.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim savePath As String = folderPath & "\ExportedPanel.png"
Dim ResizedImage As Image = ResizeImage(DrawControlToBitmap(Panel1), 600, 600)
SmallImage = SaveImage(ResizedImage)
ResizedImage.Save(savePath, ImageFormat.Png)
bitmap.Dispose()
Process.Start(savePath)
End Using
End Sub
Private Shared Function DrawControlToBitmap(ByVal control As Control) As Bitmap
Dim bitmap As New Bitmap(control.Width, control.Height)
Dim graphics As Graphics = Graphics.FromImage(bitmap)
Dim rect As Rectangle = control.RectangleToScreen(control.ClientRectangle)
graphics.CopyFromScreen(rect.Location, Point.Empty, control.Size)
Return bitmap
End Function
Private Function ResizeImage(ByRef InputImage As Image, MaxWidth As Integer, MaxHeight As Integer) As Image
'ReSize the Image if needed to save space in the database
If MaxWidth < InputImage.Width Or MaxHeight < InputImage.Height Then
'ReSize Image
Dim Scale As Double = Math.Min(MaxWidth / InputImage.Width, MaxHeight / InputImage.Height)
Return New Bitmap(InputImage, New Size(Math.Round(InputImage.Width * Scale), Math.Round(InputImage.Height * Scale)))
Else
'Image size was OK
Return InputImage
End If
End Function
Private Function SaveImage(TheImage As Image) As Byte()
'Converts Image to Byte Array
Using mStream As New MemoryStream()
'TheImage.Save(mStream, TheImage.RawFormat)
TheImage.Save(mStream, ImageFormat.Png)
Return mStream.ToArray()
End Using
End Function
End Class