Créer une image à partir d'un controle

Résolu
bossun Messages postés 173 Date d'inscription lundi 3 mars 2003 Statut Membre Dernière intervention 15 décembre 2010 - 7 juin 2006 à 14:00
bossun Messages postés 173 Date d'inscription lundi 3 mars 2003 Statut Membre Dernière intervention 15 décembre 2010 - 7 juin 2006 à 16:14
Salut,

Je voudrais créer une image depuis un controle et l'exporter dans un jpg, bmp, ou png (c'est égal)

Dans mon cas il s'agit d'un graphique créé à partir d'un composant ActivX....

J'ai essayé d'utiliser la calsse Graphics avec la méthode CreateGraphics() de mon composant mais sans succès....

Bossun

P.S1  C'est mieux de prendre son pied que de se prendre la tête!

/FONT>

7 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
7 juin 2006 à 14:57
Je pense que la technique précédente est bonne mais il y a plus simple. ( .NET 2.0 )

// ax c'est l'ActiveX.
Bitmap bmp = new Bitmap( ax.Width, ax.Height );


using ( Graphics g = Graphics.FromImage( bmp ) )
{
    g.CopyFromScreen
    (
        ax.PointToScreen( new Point( 0, 0 ) ),
        new Point( 0, 0 ),
        ax.ClientRectangle.Size
    );
}


bmp.Save( "C:\\copy.jpg", System.Drawing.Imaging.ImageFormat.Jpeg );
3
cs_hadhber Messages postés 37 Date d'inscription dimanche 31 juillet 2005 Statut Membre Dernière intervention 27 septembre 2007
7 juin 2006 à 14:12
[StructLayout(LayoutKind.Sequential)]







private



struct
RECT{


public



int
Left;


public



int
Top;


public



int
Right;


public



int
Bottom;}



[DllImport("GDI32")]







private



static



extern



int
DeleteDC(IntPtr hdc);


[DllImport("user32")]







private



static



extern



long
ReleaseDC(IntPtr hwnd, IntPtr hdc);


[DllImport("gdi32")]







private



static



extern



int
DeleteObject(IntPtr hObj);


[DllImport("gdi32")]







private



static



extern



int
CreateCompatibleDC(IntPtr hDC);


[DllImport("user32")]







private



static



extern



int
GetWindowDC(IntPtr hwnd);


[DllImport("gdi32")]







private



static



extern



int
SelectObject(IntPtr hDC, IntPtr hObject);


[DllImport("gdi32")]







private



static



extern



int
CreateCompatibleBitmap(IntPtr hDC, IntPtr nWidth, IntPtr nHeight);


[DllImport("user32")]







private



static



extern
System.Int32 GetWindowRect(IntPtr hwnd,

ref
RECT lpRect);


[DllImport("user32")]







private



static



extern



bool
IsWindow(IntPtr hwnd);


[DllImport("gdi32.dll")]







private



static



extern



bool
BitBlt(IntPtr hdcDest,

int
nXDest,

int
nYDest,

int
nWidth,

int
nHeight, IntPtr hdcSrc,

int

nXSrc,




int
nYSrc, System.Int32 dwRop);







public



static
Bitmap GrabWindow(IntPtr hWnd){


try

{







long
hWindowDC, hOffscreenDC, nWidth, nHeight, hBitmap, hOldBmp;RECT rec;

rec =


new
RECT();Bitmap MyBitmap;

MyBitmap =


new
Bitmap(640, 480);


if
((hWnd.ToInt32() != 0) && (IsWindow(hWnd))){

hWindowDC = GetWindowDC(hWnd);


if
(hWindowDC.ToString() !=

null
){


if
(GetWindowRect(hWnd,

ref
rec).ToString() !=

null
){

nWidth = rec.Right - rec.Left;

nHeight = rec.Bottom - rec.Top;

hOffscreenDC = CreateCompatibleDC(


new
System.IntPtr(hWindowDC));


if
(hOffscreenDC.ToString() !=

null
){

hBitmap = CreateCompatibleBitmap(


new
System.IntPtr(hWindowDC),

new
System.IntPtr(nWidth),

new
System.IntPtr(nHeight));


if
(hBitmap.ToString() !=

null
){

hOldBmp = SelectObject(


new
System.IntPtr(hOffscreenDC),

new
System.IntPtr(hBitmap));BitBlt(


new
System.IntPtr(hOffscreenDC), 0, 0, (

int
)nWidth, (

int
)nHeight,

new
System.IntPtr(hWindowDC), 0, 0, 13369376);MyBitmap = Bitmap.FromHbitmap(


new
IntPtr(hBitmap));DeleteObject(


new
System.IntPtr((SelectObject(

new
System.IntPtr(hOffscreenDC),

new
System.IntPtr(hOldBmp)))));}

DeleteDC(


new
System.IntPtr(hOffscreenDC));}

}

ReleaseDC(hWnd,


new
System.IntPtr(hWindowDC));GC.Collect();

}

}


return
MyBitmap;}


catch
(Exception ex){

System.Console.WriteLine (ex.Message);


return



null
;}

}



 
0
cs_hadhber Messages postés 37 Date d'inscription dimanche 31 juillet 2005 Statut Membre Dernière intervention 27 septembre 2007
7 juin 2006 à 14:14
tu met le code que j'ai envoyé dans une classe et tu appelle la fonction statique GrabWindow (MyControl.Handle)
Bonne Chance
0
bossun Messages postés 173 Date d'inscription lundi 3 mars 2003 Statut Membre Dernière intervention 15 décembre 2010 1
7 juin 2006 à 14:21
je vais essayer ça tout de suite..

merci en tout cas

Bossun

P.S1  C'est mieux de prendre son pied que de se prendre la tête!

/FONT>
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
bossun Messages postés 173 Date d'inscription lundi 3 mars 2003 Statut Membre Dernière intervention 15 décembre 2010 1
7 juin 2006 à 14:55
partout où il y a  [DllImport(




"..."
)] j'ai l'erreur suivante:

Erreur 3 Le type ou le nom d'espace de noms 'DllImport' est introuvable (une directive using ou une référence d'assembly est-elle manquante ?)

il me manque un using au sommet de ma classe?

Bossun

P.S1  C'est mieux de prendre son pied que de se prendre la tête!

/FONT>
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
7 juin 2006 à 15:00
"il me manque un using au sommet de ma classe?"









using System.Runtime.InteropServices;
0
bossun Messages postés 173 Date d'inscription lundi 3 mars 2003 Statut Membre Dernière intervention 15 décembre 2010 1
7 juin 2006 à 16:14
merci beaucoup

Bossun

P.S1  C'est mieux de prendre son pied que de se prendre la tête!

/FONT>
0
Rejoignez-nous