Assembly : System.Drawing (dans system.drawing.dll)
SyntaxePublic Sub DrawImage ( _ image As Image, _ destRect As Rectangle, _ srcX As Single, _ srcY As Single, _ srcWidth As Single, _ srcHeight As Single, _ srcUnit As GraphicsUnit, _ imageAttrs As ImageAttributes, _ callback As DrawImageAbort, _ callbackData As IntPtr _ )
Dim instance As Graphics Dim image As Image Dim destRect As Rectangle Dim srcX As Single Dim srcY As Single Dim srcWidth As Single Dim srcHeight As Single Dim srcUnit As GraphicsUnit Dim imageAttrs As ImageAttributes Dim callback As DrawImageAbort Dim callbackData As IntPtr instance.DrawImage(image, destRect, srcX, srcY, srcWidth, srcHeight, srcUnit, imageAttrs, callback, callbackData)
public void DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData )
public: void DrawImage ( Image^ image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes^ imageAttrs, DrawImageAbort^ callback, IntPtr callbackData )
public void DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData )
public function DrawImage ( image : Image, destRect : Rectangle, srcX : float, srcY : float, srcWidth : float, srcHeight : float, srcUnit : GraphicsUnit, imageAttrs : ImageAttributes, callback : DrawImageAbort, callbackData : IntPtr )
Paramètres
- image
Image à dessiner.
- destRect
Structure Rectangle qui spécifie l'emplacement et la taille de l'image dessinée. L'image est ajustée de manière à tenir à l'intérieur du rectangle.
- srcX
Coordonnée x de l'angle supérieur gauche de la partie de l'image source à dessiner.
- srcY
Coordonnée y de l'angle supérieur gauche de la partie de l'image source à dessiner.
- srcWidth
Largeur de la partie de l'image source à dessiner.
- srcHeight
Hauteur de la partie de l'image source à dessiner.
- srcUnit
Membre de l'énumération GraphicsUnit qui spécifie les unités de mesure permettant de déterminer le rectangle source.
- imageAttrs
ImageAttributes qui spécifie des informations de recoloriage et de gamma pour l'objet image.
- callback
Délégué Graphics.DrawImageAbort qui spécifie une méthode à appeler lors du dessin de l'image. Cette méthode est fréquemment appelée pour vérifier si l'exécution de la méthode DrawImage doit être arrêtée selon des critères déterminés par l'application.
- callbackData
Valeur spécifiant d'autres données pour le délégué Graphics.DrawImageAbort à utiliser pour vérifier si l'exécution de la méthode DrawImage doit être arrêtée.
NotesLes paramètres srcX, srcY, srcWidth et srcHeight spécifient une partie rectangulaire de l'objet image à dessiner. Le rectangle est fonction de l'angle supérieur gauche de l'image source. Cette partie est ajustée de manière à tenir à l'intérieur du rectangle spécifié par le paramètre destRect.
Cette surcharge avec les paramètres callback et callbackData permet d'arrêter le dessin d'une image qui a commencé selon des critères et des données déterminés par l'application. Si, par exemple, une application commence à dessiner une image grand format et si l'utilisateur fait défiler l'image en dehors de l'écran, l'application arrête alors le dessin.
ExempleL'exemple de code suivant est destiné à être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d'événements Paint. Le code commence par définir une méthode de rappel pour le délégué Graphics.DrawImageAbort ; la définition est simpliste et se contente de vérifier si la méthode DrawImage l'appelle avec un paramètre callBackData null. Le corps de l'exemple permet d'effectuer les opérations suivantes :
-
Crée une instance de la méthode de rappel Graphics.DrawImageAbort.
-
Crée une image à partir d'un fichier JPEG, SampImag.jpg, dans le dossier de l'exemple.
-
Crée des points qui définissent un rectangle de destination dans lequel dessiner l'image.
-
Crée un rectangle source pour sélectionner la partie de l'image à dessiner.
-
Définit le pixel comme unité de dessin graphique.
-
Dessine l'image d'origine à l'écran.
-
Crée un autre rectangle de destination dans lequel dessiner une image ajustée.
-
Crée et définit les attributs de l'image ajustée pour obtenir une valeur de gamma supérieure à la normale.
-
Dessine l'image ajustée à l'écran.
Concernant le rectangle de destination non ajusté d'origine, la position situe l'image à l'écran, tandis que la taille du rectangle source et la taille et la forme du rectangle de destination déterminent la mise à l'échelle de l'image dessinée.
Comme cet exemple utilise une surcharge qui passe un paramètre callBackData, le rappel de Graphics.DrawImageAbort retourne false, ce qui provoque la poursuite de la méthode DrawImage, et l'exemple dessine l'image ajustée à l'écran.
Private Function DrawImageCallback8(ByVal callBackData As IntPtr) As Boolean ' Test for call that passes callBackData parameter. If callBackData.Equals(IntPtr.Zero) Then ' If no callBackData passed, abort DrawImage method. Return True Else ' If callBackData passed, continue DrawImage method. Return False End If End Function Public Sub DrawImageRect4FloatAttribAbortData(ByVal e As PaintEventArgs) ' Create callback method. Dim imageCallback As New _ Graphics.DrawImageAbort(AddressOf DrawImageCallback8) Dim imageCallbackData As New IntPtr(1) ' Create image. Dim newImage As Image = Image.FromFile("SampImag.jpg") ' Create rectangle for displaying original image. Dim destRect1 As New Rectangle(100, 25, 450, 150) ' Create coordinates of rectangle for source image. Dim x As Single = 50.0F Dim y As Single = 50.0F Dim width As Single = 150.0F Dim height As Single = 150.0F Dim units As GraphicsUnit = GraphicsUnit.Pixel ' Draw original image to screen. e.Graphics.DrawImage(newImage, destRect1, x, y, width, _ height, units) ' Create rectangle for adjusted image. Dim destRect2 As New Rectangle(100, 175, 450, 150) ' Create image attributes and set large gamma. Dim imageAttr As New ImageAttributes imageAttr.SetGamma(4.0F) ' Draw adjusted image to screen. Try ' Draw adjusted image to screen. e.Graphics.DrawImage(newImage, destRect2, x, y, width, _ height, units, imageAttr, imageCallback, imageCallbackData) Catch ex As Exception e.Graphics.DrawString(ex.ToString(), New Font("Arial", 8), _ Brushes.Black, New PointF(0, 0)) End Try End Sub
// Define DrawImageAbort callback method. private bool DrawImageCallback8(IntPtr callBackData) { // Test for call that passes callBackData parameter. if(callBackData==IntPtr.Zero) { // If no callBackData passed, abort DrawImage method. return true; } else { // If callBackData passed, continue DrawImage method. return false; } } public void DrawImageRect4FloatAttribAbortData(PaintEventArgs e) { // Create callback method. Graphics.DrawImageAbort imageCallback = new Graphics.DrawImageAbort(DrawImageCallback8); IntPtr imageCallbackData = new IntPtr(1); // Create image. Image newImage = Image.FromFile("SampImag.jpg"); // Create rectangle for displaying original image. Rectangle destRect1 = new Rectangle(100, 25, 450, 150); // Create coordinates of rectangle for source image. float x = 50.0F; float y = 50.0F; float width = 150.0F; float height = 150.0F; GraphicsUnit units = GraphicsUnit.Pixel; // Draw original image to screen. e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units); // Create rectangle for adjusted image. Rectangle destRect2 = new Rectangle(100, 175, 450, 150); // Create image attributes and set large gamma. ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetGamma(4.0F); // Draw adjusted image to screen. try { checked { // Draw adjusted image to screen. e.Graphics.DrawImage( newImage, destRect2, x, y, width, height, units, imageAttr, imageCallback, imageCallbackData); } } catch (Exception ex) { e.Graphics.DrawString( ex.ToString(), new Font("Arial", 8), Brushes.Black, new PointF(0, 0)); } }
// Define DrawImageAbort callback method. private: bool DrawImageCallback8( IntPtr callBackData ) { // Test for call that passes callBackData parameter. if ( callBackData == IntPtr::Zero ) { // If no callBackData passed, abort DrawImage method. return true; } else { // If callBackData passed, continue DrawImage method. return false; } } public: void DrawImageRect4FloatAttribAbortData( PaintEventArgs^ e ) { // Create callback method. Graphics::DrawImageAbort^ imageCallback = gcnew Graphics::DrawImageAbort( this, &Form1::DrawImageCallback8 ); IntPtr imageCallbackData = IntPtr(1); // Create image. Image^ newImage = Image::FromFile( "SampImag.jpg" ); // Create rectangle for displaying original image. Rectangle destRect1 = Rectangle(100,25,450,150); // Create coordinates of rectangle for source image. float x = 50.0F; float y = 50.0F; float width = 150.0F; float height = 150.0F; GraphicsUnit units = GraphicsUnit::Pixel; // Draw original image to screen. e->Graphics->DrawImage( newImage, destRect1, x, y, width, height, units ); // Create rectangle for adjusted image. Rectangle destRect2 = Rectangle(100,175,450,150); // Create image attributes and set large gamma. ImageAttributes^ imageAttr = gcnew ImageAttributes; imageAttr->SetGamma( 4.0F ); // Draw adjusted image to screen. try { // Draw adjusted image to screen. e->Graphics->DrawImage( newImage, destRect2, x, y, width, height, units, imageAttr, imageCallback, imageCallbackData ); } catch ( Exception^ ex ) { e->Graphics->DrawString( ex->ToString(), gcnew System::Drawing::Font( "Arial",8 ), Brushes::Black, PointF(0,0) ); } }
// Define DrawImageAbort callback method. private boolean DrawImageCallback8(IntPtr callBackData) { // Test for call that passes callBackData parameter. if (callBackData.Equals( IntPtr.Zero)) { // If no callBackData passed, abort DrawImage method. return true ; } else { // If callBackData passed, continue DrawImage method. return false ; } } //DrawImageCallback8 public void DrawImageRect4FloatAttribAbortData(PaintEventArgs e) { // Create callback method. Graphics.DrawImageAbort imageCallback = new Graphics.DrawImageAbort( DrawImageCallback8); IntPtr imageCallbackData = new IntPtr(1); // Create image. Image newImage = Image.FromFile("SampImag.jpg"); // Create rectangle for displaying original image. Rectangle destRect1 = new Rectangle(100, 25, 450, 150); // Create coordinates of rectangle for source image. float x = 50; float y = 50; float width = 150; float height = 150; GraphicsUnit units = GraphicsUnit.Pixel; // Draw original image to screen. e.get_Graphics().DrawImage(newImage, destRect1, x, y, width, height, units); // Create rectangle for adjusted image. Rectangle destRect2 = new Rectangle(100, 175, 450, 150); // Create image attributes and set large gamma. ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetGamma(4); try { // Draw adjusted image to screen. e.get_Graphics().DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr, imageCallback, imageCallbackData); } catch(System.Exception ex) { e.get_Graphics().DrawString(ex.ToString(), new Font("Arial", 8), Brushes.get_Black(), new PointF(0, 0)); } } //DrawImageRect4FloatAttribAbortData
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Voir aussiRéférence
Graphics, classeMembres Graphics
System.Drawing, espace de noms
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ