Question

J'ai utilisé une fois bitblt pour enregistrer une capture d'écran dans un fichier image (.NET Compact Framework v3.5, Windows Mobile 2003 et ultérieurement). Fonctionnait bien. Maintenant, je veux dessiner un bitmap sur un formulaire. je pourrais utiliser this.CreateGraphics().DrawImage(mybitmap, 0, 0), mais je me demandais si cela fonctionnerait avec Bitblt comme avant et échanger les paramètres. Alors j'ai écrit:

[DllImport("coredll.dll")]
public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

(Et plus bas :)

IntPtr hb = mybitmap.GetHbitmap();
BitBlt(this.Handle, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);

Mais le formulaire reste blanc uni. Pourquoi donc? Où est l'erreur que j'ai engagée? Merci pour vos opinions. Bravo, David

Était-ce utile?

La solution

this.Handle est un Poignée de fenêtre pas un Contexte de l'appareil.

Remplacer this.Handle avec this.CreateGraphics().GetHdc()

Bien sûr, vous devrez détruire l'objet graphique, etc.

IntPtr hb = mybitmap.GetHbitmap(); 
using (Graphics gfx = this.CreateGraphics())
{
  BitBlt(gfx.GetHdc(), 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
}

en outre hb est un Bitmap Handle pas un device context Donc, l'extrait ci-dessus ne fonctionnera toujours pas. Vous devrez créer un contexte d'appareil à partir du bitmap:

    using (Bitmap myBitmap = new Bitmap("c:\test.bmp"))
    {
        using (Graphics gfxBitmap = Graphics.FromImage(myBitmap))
        {
            using (Graphics gfxForm = this.CreateGraphics())
            {
                IntPtr hdcForm = gfxForm.GetHdc();
                IntPtr hdcBitmap = gfxBitmap.GetHdc();
                BitBlt(hdcForm, 0, 0, myBitmap.Width, myBitmap.Height, hdcBitmap, 0, 0, 0x00CC0020);
                gfxForm.ReleaseHdc(hdcForm);
                gfxBitmap.ReleaseHdc(hdcBitmap);
            }
        }
    }

Autres conseils

Vous voulez dire quelque chose dans ce sens?

    public void CopyFromScreen(int sourceX, int sourceY, int destinationX, 
                               int destinationY, Size blockRegionSize, 
                               CopyPixelOperation copyPixelOperation)
    {
        IntPtr desktopHwnd = GetDesktopWindow();
        if (desktopHwnd == IntPtr.Zero)
        {
            throw new System.ComponentModel.Win32Exception();
        }
        IntPtr desktopDC = GetWindowDC(desktopHwnd);
        if (desktopDC == IntPtr.Zero)
        {
            throw new System.ComponentModel.Win32Exception();
        }
        if (!BitBlt(hDC, destinationX, destinationY, blockRegionSize.Width, 
             blockRegionSize.Height, desktopDC, sourceX, sourceY, 
             copyPixelOperation))
        {
            throw new System.ComponentModel.Win32Exception();
        }
        ReleaseDC(desktopHwnd, desktopDC);
    }

Pour info, c'est tout de suite du SDF.

EDIT: Ce n'est pas vraiment clair dans cet extrait, mais HDC dans le BITBLT est le HDC du bitmap cible (dans lequel vous souhaitez peindre).

Êtes-vous sûr que this.Handle fait référence à un contexte de dispositif valide? Avez-vous essayé de vérifier la valeur de retour du BitBlt fonction?

Essayez ce qui suit:

[DllImport("coredll.dll", EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("coredll.dll", EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr hwnd);

IntPtr hdc     = GetDC(this.Handle);
IntPtr hdcComp = CreateCompatibleDC(hdc);

BitBlt(hdcComp, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top