Accueil > > > WRAPPER .NET 2.0 POUR LES CACHEDBITMAP DE GDI+
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.
Historique
- 27 mars 2008 12:22:21 :
- Petit bug.
Sources du même auteur
Sources de la même categorie
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
|
Derniers Blogs
[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko [FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|