02 septiembre, 2012

Funciones útiles para manejar gráficos en VB.net usando GDI+


Bueno, no necesita demasiada explicación, se trata de funciones comunes como leer un archivo, redimensionarlo, dibujar una imagen sobre otra etc... muy aconsejable usar este tipo de funciones para evitar redibujar las imágenes en resolución máxima todo el tiempo a la hora de programar un juego. Es una buena idea dibujar en un caché cada capa de sprites para poder acoplarlas rápidamente, ya que GDI+ no es una opción especialmente rápida.

'leer una imágen desde archivo
Public Function read_image(ByVal file As String) As System.Drawing.Bitmap
        Dim img As New Bitmap(file)
        Return img
    End Function
    Public Function read_image(ByVal file As String, ByVal sizex As Integer, ByVal sizey As Integer) As System.Drawing.Bitmap
        Dim img As New Bitmap(file)
        Dim b As New Bitmap(sizex, sizey)
        Dim bg As Graphics = Graphics.FromImage(b)
        bg.DrawImage(img, New Rectangle(New Point(0, 0), New Size(sizex, sizey)), New Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel)
        Return b
    End Function
'redimensionar una imágen
    Public Function resize_bmp(ByVal img As Bitmap, ByVal sizex As Integer, ByVal sizey As Integer) As Bitmap
        Dim b As New Bitmap(sizex, sizey)
        Dim bg As Graphics = Graphics.FromImage(b)
        bg.DrawImage(img, New Rectangle(New Point(0, 0), New Size(sizex, sizey)), New Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel)
        Return b
    End Function
    Public Function resize_bmp(ByVal img As Bitmap, ByVal size As Size) As Bitmap
        Dim b As New Bitmap(size.Width, size.Height)
        Dim bg As Graphics = Graphics.FromImage(b)
        bg.DrawImage(img, New Rectangle(New Point(0, 0), New Size(size.Width, size.Height)), New Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel)
        Return b
    End Function
    ' extraer una porción de una imágen
    ' image = imagen a recortar
    ' cuadriculax y cuadriculay = número de sectores por cada dimensión
    ' sectorx y sectory = vector puntero de la selección
    Public Function get_portion(ByVal image As System.Drawing.Bitmap, ByVal cuadriculax As Short, ByVal cuadriculay As Short, ByVal sectorx As Integer, ByVal sectory As Integer) As System.Drawing.Bitmap
        Dim img As New Bitmap(CInt(image.Size.Width / cuadriculax), CInt(image.Size.Height / cuadriculay))
        'Dim b As New Bitmap(CInt(image.Size.Width / cuadriculax), CInt(image.Size.Height / cuadriculay))
        Dim bg As Graphics = Graphics.FromImage(img)
        Dim tmpx As Integer = (image.Size.Width / cuadriculax) * sectorx
        Dim tmpy As Integer = (image.Size.Height / cuadriculay) * sectory
        Dim port As New Rectangle(New System.Drawing.Point(0, 0), New Size(New Point(image.Size.Width / cuadriculax, image.Size.Height / cuadriculay)))
        bg.DrawImage(image, port, tmpx, tmpy, CInt(port.Size.Width), CInt(port.Size.Height), GraphicsUnit.Pixel)
        Return img
    End Function
    Public Function get_portion(ByVal image As System.Drawing.Bitmap, ByVal cuadricula As System.Drawing.Size, ByVal sector As System.Drawing.Size) As System.Drawing.Bitmap
        Dim img As New Bitmap(CInt(image.Size.Width / cuadricula.Width), CInt(image.Size.Height / cuadricula.Height))
        'Dim b As New Bitmap(CInt(image.Size.Width / cuadricula.Width), CInt(image.Size.Height / cuadricula.Height))
        Dim bg As Graphics = Graphics.FromImage(img)
        Dim tmpx As Integer = (image.Size.Width / cuadricula.Width) * sector.Width
        Dim tmpy As Integer = (image.Size.Height / cuadricula.Height) * sector.Height
        Dim port As New Rectangle(New System.Drawing.Point(0, 0), New Size(New Point(image.Size.Width / cuadricula.Width, image.Size.Height / cuadricula.Height)))
        bg.DrawImage(image, port, tmpx, tmpy, CInt(port.Size.Width), CInt(port.Size.Height), GraphicsUnit.Pixel)
        Return img
    End Function
' sobredibujar una imágen sobre otra
    Public Function layer_sum(ByVal layer1 As Bitmap, ByVal layer2 As Bitmap) As Bitmap
        Dim bg As Graphics = Graphics.FromImage(layer1)
        bg.DrawImage(layer2, New Point(0, 0))
        Return layer1
    End Function
    Public Function layer_sum(ByVal layer1 As Bitmap, ByVal layer2 As Bitmap, ByVal offsetx As Integer, ByVal offsety As Integer) As Bitmap
        Dim bg As Graphics = Graphics.FromImage(layer1)
        bg.DrawImage(layer2, New Point(offsetx, offsety))
        Return layer1
    End Function
' dibujar una imágen de texto rasterizado
    Public Function get_text_layer(ByVal size As System.Drawing.Size, ByVal text As String) As System.Drawing.Bitmap
        Dim img As New Bitmap(size.Width, size.Height)
        Dim bg As Graphics = Graphics.FromImage(img)
        bg.DrawString(text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, New Point(1, -1))
        bg.DrawString(text, New Font("Arial", 12, FontStyle.Bold), Brushes.White, New Point(0, 0))
        Return img
    End Function
    Public Function get_text_layer(ByVal sizex As Integer, ByVal sizey As Integer, ByVal text As String) As System.Drawing.Bitmap
        Dim img As New Bitmap(sizex, sizey)
        Dim bg As Graphics = Graphics.FromImage(img)
        bg.DrawString(text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, New Point(1, -1))
        bg.DrawString(text, New Font("Arial", 12, FontStyle.Bold), Brushes.White, New Point(0, 0))
        Return img
    End Function
    Public Function get_text_layer(ByVal sizex As Integer, ByVal sizey As Integer, ByVal text As String, ByVal fontsize As Integer) As System.Drawing.Bitmap
        Dim img As New Bitmap(sizex, sizey)
        Dim bg As Graphics = Graphics.FromImage(img)
        bg.DrawString(text, New Font("Arial", fontsize, FontStyle.Bold), Brushes.Black, New Point(1, -1))
        bg.DrawString(text, New Font("Arial", fontsize, FontStyle.Bold), Brushes.White, New Point(0, 0))
        Return img
    End Function 'code by abderramah abdlab software
Publicar un comentario en la entrada