Jump to content

Redimensionar Imagem


bigboy123
 Share

Recommended Posts

Boas pessoal,

Aqui à tempos realizei um programa que tirava printscreens assim que era executado.

Agora quero redimensionar as imagens que o programa tira, mas não sei como  😕

Aqui vai o código do principio ao fim:


    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer

    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer

    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer

    Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer

    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer

    Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer

    Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer


    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer

    Const SRCCOPY As Integer = &HCC0020

    Private Background As Bitmap

    Private fw, fh As Integer

----------------------------------------------------------------------------------------------------------------

    Protected Sub CaptureScreen()


        Dim hsdc, hmdc As Integer

        Dim hbmp, hbmpold As Integer

        Dim r As Integer


        hsdc = CreateDC("DISPLAY", "", "", "")

        hmdc = CreateCompatibleDC(hsdc)


        fw = GetDeviceCaps(hsdc, 8)

        fh = GetDeviceCaps(hsdc, 10)

        hbmp = CreateCompatibleBitmap(hsdc, fw, fh)


        hbmpold = SelectObject(hmdc, hbmp)

        r = BitBlt(hmdc, 0, 0, fw, fh, hsdc, 0, 0, 13369376)



        hbmp = SelectObject(hmdc, hbmpold)


        r = DeleteDC(hsdc)

        r = DeleteDC(hmdc)



        Background = Image.FromHbitmap(New IntPtr(hbmp))

        DeleteObject(hbmp)

    End Sub

-----------------------------------------------------------------------------------------------------------------

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

------------------------------------------------------------------------------------------------------------------

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        CaptureScreen()

        Background.Save("\PrintScreen.jpeg")

No evento do Timer1 o CaptureScreen() é o que faz o PrintScreen e asseguir a isso queria redimensionar a variável "Background" (que é a imagem do PrintScreen) para depois continuar com o "Save" ( guarda a imagem no directório ).

Link to comment
Share on other sites

Oh diabo, e precisas lá de ir falar directamente com a GDI!

A framework é o intermediário perfeito.

        Dim Fonte As Bitmap = Me.Icon.ToBitmap
        Dim Alvo As New Bitmap(200, 200)

        Dim g As Graphics = Graphics.FromImage(Alvo)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(Fonte, New Rectangle(0, 0, Alvo.Width, Alvo.Height))
        Alvo.Save("c:\grande.bmp")

Sendo "Fonte" a imagem base, que pode ser obtida de todas as formas permitidas na classe "Bitmap" e Bitmap(200,200) os novos valores para o bitmap.

Só precisas de aplicar um codec no método Save para ires de encontro ao formato que pretendes.

Lá mais para o final do ano vou libertar aqui uma biblioteca de manipulação de imagem, à partida com código aberto, e vais ver que a framework tem quase tudo o que precisas 😕

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Eu quero usar a GDI porque apartir do framework ah sistemas que a printscreen fica a preto e não mostra a imagem que foi tirada.

Nunca me aconteceu. A não ser que estejas a tentar capturar video comprimido.

Quanto à dúvida, um dos overloads do construtor da classe Bitmap aceita Image.

Basta portanto substituír:

Dim Fonte As Bitmap = Me.Icon.ToBitmap

por

Dim b As New Bitmap(Background)

Mas, segundo o teu código, a tua Background até já é um bitmap... logo basta:

Dim Fonte As Bitmap = Background

Ou isso ou trocar todos os sítios onde tem "Fonte" para "Background"

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Com "Dim Fonte As Bitmap = Background" da-me erro na linha de código "g.DrawImage(Fonte, New Rectangle(0, 0, Alvo.Width, Alvo.Height))" (Object reference not set to an instance of an object.)

Mas de qualquer maneira se com a primeira resposta conseguires em vez do icon capturar a imagem do screen agradecia-te  😕

Link to comment
Share on other sites

Se te vem isso é porque a tua variável "Background" vem vazia.

Verifica se não é isso que acontece, através de step-debug.

Basta que a variável "Fonte" represente um Bitmap válido e não nulo para que funcione na perfeição.

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.