Cut PNG file or any image
-
:-D i have a png file which includes 20 different icon. i load it into a image variable but i have no idea how i can cut icons from there. Example Code Dim img As Image img = img.FromFile("Icons.png") 'i need an method like Getimage (x, y, Width, Height) as image dim newImg as Image newImg =img.GetImage(x, y, Width, Height) Any Idea? thanks.
-
:-D i have a png file which includes 20 different icon. i load it into a image variable but i have no idea how i can cut icons from there. Example Code Dim img As Image img = img.FromFile("Icons.png") 'i need an method like Getimage (x, y, Width, Height) as image dim newImg as Image newImg =img.GetImage(x, y, Width, Height) Any Idea? thanks.
Your GetImage function could look like the following:
Public Function Getimage(ByVal sourceImage As Image, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Image Dim result As Bitmap = New Bitmap(width, height) Dim g As Graphics Try g = Graphics.FromImage(result) g.DrawImageUnscaled(sourceImage, -x, -y) Finally If Not g Is Nothing Then g.Dispose() End If End Try Return result End Function
Havent tested the code but I think it should work. In general it just generates an empty Bitmap, creates a Graphics object and draws the original image at a location so that only the needed part is drawn. There are probably more efficient ways out there, but it should do fine this way for smaller images.