Afficher une image du presse papier sur une PictureBox V.B 2010

Fermé
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 - 25 juin 2014 à 14:47
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 - 25 juin 2014 à 18:29
Bonjour, Voilà le soucis à six sous.
Je débute en Visual Basic 2010 et je cherche à afficher une image du presse papier
dans une PictureBox .
Tout ce que j'ai trouvé sur le sujet comme code source est soit incomplet soit
pas compatible avec VB2010.
Ces tordus de chez Microsoft ne sont pas foutus d'uniformiser leurs commandes
d'une version à l'autre.
Tout ce que j'ai besoin pour l'instant c'est d'un code source type pour une Windows form
et une PictureBox.
Si vous aviez la gentillesse d'aider un pauvre newbee comme moi sa me ferais plaisir .
Sa fait trois jours que je suis dessus .
Merci d'avance



3 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
25 juin 2014 à 17:54
Bonjour,

Ce que tu cherches a faire ce fait avec la touche Imprécran du clavier. Il suffit de mettre une PictureBox, un label et ce code:

Option Strict On
Imports System.Runtime.InteropServices
Public Class Form1
    Private Const WM_DRAWCLIPBOARD As Integer = &H308
    Private Const WM_CHANGECBCHAIN As Integer = &H30D
    Private mNextClipBoardViewerHWnd As IntPtr
    Private Event OnClipboardChanged()
    <DllImport("user32")> _
    Private Shared Function SetClipboardViewer(ByVal hWnd As IntPtr) As IntPtr
    End Function
    <DllImport("user32")> _
    Private Shared Function ChangeClipboardChain(ByVal hWnd As IntPtr, ByVal hWndNext As IntPtr) As _
 <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("user32")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, _
 ByVal lParam As IntPtr) As IntPtr
    End Function
    Sub New()
        InitializeComponent()
        mNextClipBoardViewerHWnd = SetClipboardViewer(Me.Handle)
        AddHandler Me.OnClipboardChanged, AddressOf ClipBoardChanged
    End Sub
    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_DRAWCLIPBOARD
                RaiseEvent OnClipboardChanged()
                SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
            Case WM_CHANGECBCHAIN
                If m.WParam.Equals(mNextClipBoardViewerHWnd) Then
                    mNextClipBoardViewerHWnd = m.LParam
                Else
                    SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
                End If
        End Select
        MyBase.WndProc(m)
    End Sub
    Private Sub ClipBoardChanged()

        If Clipboard.ContainsText = True Then
            Label1.Text = Clipboard.GetText
        End If

        If Clipboard.ContainsImage = True Then
            PictureBox1.Image = CType(Clipboard.GetImage.Clone, Image)
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

        End If
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        ChangeClipboardChain(Me.Handle, mNextClipBoardViewerHWnd)
    End Sub
End Class


Il suffit d'appuyer sur la touche Imprécran pour que la capture d'écran s'affiche.
Tu peux récupérer du texte aussi dans le label

Sinon voici un code qui met une image dans le presse papier et qui la copie dans une PictureBox. Mettre une PictureBox et un Button:

Option Strict On
Public Class Form1
    Dim pic As Image
    Dim x As New DataObject
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim ofd As New OpenFileDialog
        With ofd
            .Title = "Choisissez une image"
            .Filter = "jpeg (*.jpg)|*.jpg|gif (*.gif)|*.gif|tiff (*.tif)|*.tif|bmp (*.bmp)|*.bmp|png (*.png)|*.png"
            .FilterIndex = 1
            .RestoreDirectory = False
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                pic = New Bitmap(.FileName)
                x.SetData(DataFormats.Bitmap, False, pic)
                System.Windows.Forms.Clipboard.SetDataObject(x)
            Else
                MessageBox.Show("Opération annulée par l'utilisateur!", "Ouverture image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            .Dispose()
        End With
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        x = CType(System.Windows.Forms.Clipboard.GetDataObject, DataObject)
        pic = CType(x.GetData(DataFormats.Bitmap), Image)
        PictureBox1.Image = pic
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    End Sub
End Class


On peut aussi utiliser la touche Imprécran pour que la capture d'écran s'affiche
2
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
Modifié par papanou1965 le 25/06/2014 à 17:59
Merci c'est sympa je vais essayer cela .
Bonne soirée
0
papanou1965 Messages postés 37 Date d'inscription mardi 17 juin 2014 Statut Membre Dernière intervention 15 août 2014 1
25 juin 2014 à 18:29
Super c'est ce que je cherchais.
Merci beaucoup à toi.
Michel.
0