|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
WRAPPER .NET 2.0 POUR LES CACHEDBITMAP DE GDI+
Information sur la source
Description
Si le Framework .NET 3.0 et supérieur possèdent une classe CachedBitmap managée, ce n'est malheureusement pas le cas de .NET 2.0. Ma petite source est là pour y remédier : elle permet d'utiliser les CachedBitmap pour dessiner rapidement sur un objet Graphics. Les CachedBitmap sont censées être plus rapides à dessiner qu'en passant par la méthode Graphics.DrawImage() Suite à la demande de Kevin Ory, je me suis mis au travail et voice cette source. Elle est composée de 2 parties : une librairie ManagedCachedBitmap en C# et une petite application de test en VB .NET
Source
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Drawing;
- using System.Reflection;
-
- namespace ManagedCachedBitmap
- {
- /// <summary>
- /// Wrapper pour les CachedBitmap non managés de GDI+
- /// </summary>
- public class ManagedCachedBitmap
- {
- [DllImport("gdiplus.dll")]
- public static extern int GdipCreateCachedBitmap(IntPtr pBitmap, IntPtr pGraphics, ref IntPtr pCachedBitmap);
- [DllImport("gdiplus.dll")]
- public static extern int GdipDrawCachedBitmap(IntPtr pGraphics, IntPtr pCachedBitmap, int x, int y);
- [DllImport("gdiplus.dll")]
- public static extern int GdipDeleteCachedBitmap(IntPtr pCachedBitmap);
-
- private Bitmap _cachedBitmap;
- private Graphics _graphics;
- private IntPtr _pCachedBitmap;
- private FieldInfo _Bitmapfi;
- private FieldInfo _Graphicsfi;
- private IntPtr _pBitmapfi;
- private IntPtr _pGraphicsfi;
- private Boolean _isLoaded = false;
-
- /// <summary>
- /// Constructeur.
- /// </summary>
- /// <param name="b">un objet Bitmap</param>
- /// <param name="g">un objet Graphics</param>
- public ManagedCachedBitmap(Bitmap b, Graphics g)
- {
- this.Init(b, g);
- }
-
- /// <summary>
- /// Destructeur
- /// </summary>
- ~ManagedCachedBitmap()
- {
- this.Delete();
- }
-
- /// <summary>
- /// Utilisé pour initialiser un CachedBitmap à partir d'un Graphics et d'un Bitmap
- /// </summary>
- /// <param name="b">un objet Bitmap</param>
- /// <param name="g">un objet Graphics</param>
- public void Init(Bitmap b, Graphics g)
- {
- if (_isLoaded) this.Delete();
- this._cachedBitmap = b;
- this._graphics = g;
- this._Bitmapfi = typeof(Bitmap).GetField("nativeImage", BindingFlags.Instance | BindingFlags.NonPublic);
- this._pBitmapfi = (IntPtr)this._Bitmapfi.GetValue(this._cachedBitmap);
- this._Graphicsfi = typeof(Graphics).GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
- this._pGraphicsfi = (IntPtr)this._Graphicsfi.GetValue(this._graphics);
- GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
- this._isLoaded = true;
- }
-
- /// <summary>
- /// Modifie seulement le Bitmap sans avoir à tout refaire (plus rapide que la méthode Init() ). Utile pour faire une animation par exemple.
- /// </summary>
- /// <param name="b">Un objet Bitmap</param>
- /// <returns></returns>
- /// <remarks>La mise à jour ne sera pas effectuée si la méthode</remarks>
- public Boolean InitBitmap(Bitmap b)
- {
- if (!_isLoaded) return false;
- this.Delete();
- this._cachedBitmap = b;
- this._Bitmapfi = typeof(Bitmap).GetField("nativeImage", BindingFlags.Instance | BindingFlags.NonPublic);
- this._pBitmapfi = (IntPtr)this._Bitmapfi.GetValue(this._cachedBitmap);
- GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
- this._isLoaded = true;
- return true;
- }
-
- /// <summary>
- /// Modifie seulement le Graphics où l'on dessinera le Bitmap (plus rapide que la méthode Init() ).
- /// </summary>
- /// <param name="g"></param>
- /// <returns></returns>
- public Boolean InitGraphics(Graphics g)
- {
- if (!_isLoaded) return false;
- this.Delete();
- this._graphics = g;
- this._Graphicsfi = typeof(Graphics).GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
- this._pGraphicsfi = (IntPtr)this._Graphicsfi.GetValue(this._graphics);
- GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
- this._isLoaded = true;
- return true;
- }
-
- /// <summary>
- /// Définit ou renvoie le Bitmap utilisé
- /// </summary>
- public Bitmap Bitmap
- {
- get
- {
- return _cachedBitmap;
- }
- set
- {
- this._cachedBitmap = value;
- this.InitBitmap(this._cachedBitmap);
- }
- }
-
- /// <summary>
- /// Définit ou renvoie le Graphics utilisé
- /// </summary>
- public Graphics Graphics
- {
- get
- {
- return _graphics;
- }
- set
- {
- this._graphics = value;
- this.InitGraphics(this._graphics);
- }
- }
-
- /// <summary>
- /// Vrai si l'initialisation du CachedBitmap s'est déroulée correctement
- /// </summary>
- public bool IsLoaded
- {
- get
- {
- return _isLoaded;
- }
- }
-
- /// <summary>
- /// Dessine le Bitmap sur l'objet Graphics aux coordonnées (0 ; 0)
- /// </summary>
- /// <returns>Un entier</returns>
- public int Draw()
- {
- return this.Draw(0, 0);
- }
-
- /// <summary>
- /// Dessine le Bitmap sur l'objet Graphics aux coordonnées spécifiées
- /// </summary>
- /// <param name="x">Distance X du bord gauche du Graphics</param>
- /// <param name="y">Distance Y du bord haut du Graphics</param>
- /// <returns>Un entier</returns>
- public int Draw(int x, int y)
- {
- return GdipDrawCachedBitmap(this._pGraphicsfi, this._pCachedBitmap, x, y);
- }
-
- /// <summary>
- /// Dessine le bitmap sur l'objet Graphics au coin haut-gauche spécifié par le Point
- /// </summary>
- /// <param name="p">Un objet Point qui désigne le coin haut-gauche où sera dessiné le Bitmap</param>
- /// <returns>Un entier</returns>
- public int Draw(Point p)
- {
- return this.Draw(p.X, p.Y);
- }
-
- /// <summary>
- /// Libère les ressources utilisées par le CachedBitmap non managé.
- /// </summary>
- public void Delete()
- {
- GdipDeleteCachedBitmap(this._pCachedBitmap);
- this._isLoaded = false;
- }
-
- }
- }
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Reflection;
namespace ManagedCachedBitmap
{
/// <summary>
/// Wrapper pour les CachedBitmap non managés de GDI+
/// </summary>
public class ManagedCachedBitmap
{
[DllImport("gdiplus.dll")]
public static extern int GdipCreateCachedBitmap(IntPtr pBitmap, IntPtr pGraphics, ref IntPtr pCachedBitmap);
[DllImport("gdiplus.dll")]
public static extern int GdipDrawCachedBitmap(IntPtr pGraphics, IntPtr pCachedBitmap, int x, int y);
[DllImport("gdiplus.dll")]
public static extern int GdipDeleteCachedBitmap(IntPtr pCachedBitmap);
private Bitmap _cachedBitmap;
private Graphics _graphics;
private IntPtr _pCachedBitmap;
private FieldInfo _Bitmapfi;
private FieldInfo _Graphicsfi;
private IntPtr _pBitmapfi;
private IntPtr _pGraphicsfi;
private Boolean _isLoaded = false;
/// <summary>
/// Constructeur.
/// </summary>
/// <param name="b">un objet Bitmap</param>
/// <param name="g">un objet Graphics</param>
public ManagedCachedBitmap(Bitmap b, Graphics g)
{
this.Init(b, g);
}
/// <summary>
/// Destructeur
/// </summary>
~ManagedCachedBitmap()
{
this.Delete();
}
/// <summary>
/// Utilisé pour initialiser un CachedBitmap à partir d'un Graphics et d'un Bitmap
/// </summary>
/// <param name="b">un objet Bitmap</param>
/// <param name="g">un objet Graphics</param>
public void Init(Bitmap b, Graphics g)
{
if (_isLoaded) this.Delete();
this._cachedBitmap = b;
this._graphics = g;
this._Bitmapfi = typeof(Bitmap).GetField("nativeImage", BindingFlags.Instance | BindingFlags.NonPublic);
this._pBitmapfi = (IntPtr)this._Bitmapfi.GetValue(this._cachedBitmap);
this._Graphicsfi = typeof(Graphics).GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
this._pGraphicsfi = (IntPtr)this._Graphicsfi.GetValue(this._graphics);
GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
this._isLoaded = true;
}
/// <summary>
/// Modifie seulement le Bitmap sans avoir à tout refaire (plus rapide que la méthode Init() ). Utile pour faire une animation par exemple.
/// </summary>
/// <param name="b">Un objet Bitmap</param>
/// <returns></returns>
/// <remarks>La mise à jour ne sera pas effectuée si la méthode</remarks>
public Boolean InitBitmap(Bitmap b)
{
if (!_isLoaded) return false;
this.Delete();
this._cachedBitmap = b;
this._Bitmapfi = typeof(Bitmap).GetField("nativeImage", BindingFlags.Instance | BindingFlags.NonPublic);
this._pBitmapfi = (IntPtr)this._Bitmapfi.GetValue(this._cachedBitmap);
GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
this._isLoaded = true;
return true;
}
/// <summary>
/// Modifie seulement le Graphics où l'on dessinera le Bitmap (plus rapide que la méthode Init() ).
/// </summary>
/// <param name="g"></param>
/// <returns></returns>
public Boolean InitGraphics(Graphics g)
{
if (!_isLoaded) return false;
this.Delete();
this._graphics = g;
this._Graphicsfi = typeof(Graphics).GetField("nativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic);
this._pGraphicsfi = (IntPtr)this._Graphicsfi.GetValue(this._graphics);
GdipCreateCachedBitmap(this._pBitmapfi, this._pGraphicsfi, ref this._pCachedBitmap);
this._isLoaded = true;
return true;
}
/// <summary>
/// Définit ou renvoie le Bitmap utilisé
/// </summary>
public Bitmap Bitmap
{
get
{
return _cachedBitmap;
}
set
{
this._cachedBitmap = value;
this.InitBitmap(this._cachedBitmap);
}
}
/// <summary>
/// Définit ou renvoie le Graphics utilisé
/// </summary>
public Graphics Graphics
{
get
{
return _graphics;
}
set
{
this._graphics = value;
this.InitGraphics(this._graphics);
}
}
/// <summary>
/// Vrai si l'initialisation du CachedBitmap s'est déroulée correctement
/// </summary>
public bool IsLoaded
{
get
{
return _isLoaded;
}
}
/// <summary>
/// Dessine le Bitmap sur l'objet Graphics aux coordonnées (0 ; 0)
/// </summary>
/// <returns>Un entier</returns>
public int Draw()
{
return this.Draw(0, 0);
}
/// <summary>
/// Dessine le Bitmap sur l'objet Graphics aux coordonnées spécifiées
/// </summary>
/// <param name="x">Distance X du bord gauche du Graphics</param>
/// <param name="y">Distance Y du bord haut du Graphics</param>
/// <returns>Un entier</returns>
public int Draw(int x, int y)
{
return GdipDrawCachedBitmap(this._pGraphicsfi, this._pCachedBitmap, x, y);
}
/// <summary>
/// Dessine le bitmap sur l'objet Graphics au coin haut-gauche spécifié par le Point
/// </summary>
/// <param name="p">Un objet Point qui désigne le coin haut-gauche où sera dessiné le Bitmap</param>
/// <returns>Un entier</returns>
public int Draw(Point p)
{
return this.Draw(p.X, p.Y);
}
/// <summary>
/// Libère les ressources utilisées par le CachedBitmap non managé.
/// </summary>
public void Delete()
{
GdipDeleteCachedBitmap(this._pCachedBitmap);
this._isLoaded = false;
}
}
}
Conclusion
Après quelques tests, il apparait que la méthode des CachedBitmap est rapide, mais à peu près autant que la méthode managée disponible en .NET 2.0 Graphics.DrawImageUnscaled(). L'utilité de ma source est donc sans doute limitée mais en outre la source en elle-même est intéressante je pense.
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Historique
- 27 mars 2008 12:22:21 :
- Petit bug.
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
pourquoi le GDI+ est si lent [ par NICKO02 ]
Voila, j'ai commencé il y a peu a creer des graphiques à l'aide du GDI+ de .net.J'utilise principalement les methodes DrawString() et DrawLine().C'est
Imprimante / Configuration de la page [ par zouzounet ]
Bonjour,J'ai un petit soucis avec une impression dans un prog.J'utilise du GDI+ pour tracer un tableau, que je rempli de texte (toujours avec GDI+) et
Graphics to Bitmap [ par Developpator ]
Hello;Je cherche le moyen de sauver dans un fichier *.bmp, le contenu d'un panel, à savoir son objet graphics. Merci d'avance.
dessiner un rectangle sur les controles [ par LeGnuff ]
Bonjour !j'ai un objget Graphics associé à une formj'aurais aimé savoir s'il y avait un moyen d'utiliser la méthodeGraphics.DrawRectangle(...) en fais
Graphics et controls [ par michel_roger ]
Salut tlm,Je suis en train de créer une sorte de tooltip avec ce code : Graphics help = this.CreateGraphics();Brush brush = System.Drawing.Brushes.Bla
Creer une zone graphic [ par revlis ]
BonjourJe cherche a creer un Graphics au dessus de mon form pour y dessiner dedans...Exemples: -la liste d'une combobox-un menu contextuelsachant que
effacer une forme d'un graphics [ par godefrw ]
Bonjour, je cherche à effacer une forme (par exemple une droite) de mon objet graphics sans avoir à effacre tout mon graphics et retracer tout ce qui
afficher un dessin sur une image [ par godefrw ]
Bonjour,Je désirerais afficher un dessin au dessus d'une image. J'ai crée une pictureBox dans laquelle je charge une image. Ensuite je crée un Graphic
dessiner un graphics sur une image contenue dans une pictureBox [ par godefrw ]
Bonjour, je cherche à afficher un objet Graphics (sur lequel j'ai dessiner des formes rectangles, rond etc) par dessus une image. Mon image est conte
un evenement sur un objet Graphics [ par youess81 ]
j ai un objet graphics dessiner evec DrawLine je vous ajouter un evenement click sur la ligne est ce que je peu ajouter un evenement personaliusé sur
|
Téléchargements
Logiciels à télécharger sur le même thème :
Comparez les prix Nouvelle version
|