Suite a plusieur question dans le forum et un message privé.
voici une class static de converssion d'une image en code rtf en vu de la coller dans un RichTextBox sans passer par le press-papier.
n'oublié pas les using nécéssaire !
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;
Source / Exemple :
public static class ImageToRtf
{
[DllImportAttribute("gdiplus.dll")]
private static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize, byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
[DllImport("gdi32.dll")]
static extern bool DeleteEnhMetaFile(IntPtr _hEmf);
// http://msdn.microsoft.com/en-us/library/dd162980(VS.85).aspx
private const int MM_TEXT = 1;
private const int MM_LOMETRIC = 2;
private const int MM_HIMETRIC = 3;
private const int MM_LOENGLISH = 4;
private const int MM_HIENGLISH = 5;
private const int MM_TWIPS = 6;
private const int MM_ISOTROPIC = 7;
private const int MM_ANISOTROPIC = 8;
// Le nombre de centièmes de millimètres (0,01 mm) dans un pouce
// For more information, see GetImagePrefix() method.
private const int HMM_PER_INCH = 2540;
// Le nombre de twips dans un pouce
// For more information, see GetImagePrefix() method.
private const int TWIPS_PER_INCH = 1440;
// http://msdn.microsoft.com/en-us/library/ms534113(VS.85).aspx
private enum EmfToWmfBitsFlags
{
EmfToWmfBitsFlagsDefault = 0x00000000,
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
EmfToWmfBitsFlagsNoXORClip = 0x00000004
};
#region PRIVATE METHODE
/// <summary>
/// Ajout des paramétres de résolution
/// </summary>
/// <param name="_image">Image Source</param>
/// <param name="rich">RichtextBox de destination</param>
/// <returns></returns>
private static string GetImagePrefix(Image _image,RichTextBox rich)
{
StringBuilder _rtf = new StringBuilder();
// Résolution horizontale de la richtextbox
float xDpi;
// Résolution verticale de la richtextbox
float yDpi;
// récuperation des resolution en creant un Graphics
using (Graphics _graphics = rich.CreateGraphics())
{
xDpi = _graphics.DpiX;
yDpi = _graphics.DpiY;
}
// Calculer la largeur actuelle de l'image dans (0,01) mm
int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH);
// Calculer la hauteur actuelle de l'image dans (0,01) mm
int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH);
// Calculer la largeur de l'image cible en twips
int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH);
// Calculer la hauteur de l'image cible en twips
int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH);
// Ajout des valeur dans le rtf
_rtf.Append(@"{\pict\wmetafile8");
_rtf.Append(@"\picw");
_rtf.Append(picw);
_rtf.Append(@"\pich");
_rtf.Append(pich);
_rtf.Append(@"\picwgoal");
_rtf.Append(picwgoal);
_rtf.Append(@"\pichgoal");
_rtf.Append(pichgoal);
_rtf.Append(" ");
return _rtf.ToString();
}
/// <summary>
/// Convertion du contenu de l'image en rtf
/// </summary>
/// <param name="_image">Image Source</param>
/// <param name="rich">RichtextBox de destination</param>
/// <returns></returns>
private static string GetRtfImage(Image _image, RichTextBox rich)
{
StringBuilder _rtf = null;
MemoryStream _stream = null;
Graphics _graphics = null;
Metafile _metaFile = null;
IntPtr _hdc;
try
{
_rtf = new StringBuilder();
_stream = new MemoryStream();
// recherche du graphic de la RichTextBox
using (_graphics = rich.CreateGraphics())
{
_hdc = _graphics.GetHdc();
_metaFile = new Metafile(_stream, _hdc);
_graphics.ReleaseHdc(_hdc);
}
// recherche du graphic de l'image
using (_graphics = Graphics.FromImage(_metaFile))
{
_graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height));
}
// Recherche du handle du metafichier
IntPtr _hEmf = _metaFile.GetHenhmetafile();
//recherche la taille de buffet nécessaire
uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
// Creation des tableau de bits de la taille du buffet nécéssaire
byte[] _buffer = new byte[_bufferSize];
// Remplissage du buffet
uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
//28.05.2010
//Liberation des ressources sinon le compteur monte de 2 a chaque appel de la fonction
DeleteEnhMetaFile(_hEmf);
// ajout de chaque bytes de l'image en hexadecimal dans le StringBuilder _rtf
for (int i = 0; i < _buffer.Length; ++i)
{
_rtf.Append(String.Format("{0:X2}", _buffer[i]));
}
return _rtf.ToString();
}
finally
{
if (_graphics != null)
_graphics.Dispose();
if (_metaFile != null)
_metaFile.Dispose();
if (_stream != null)
{
_stream.Close();
_stream.Dispose();
}
}
}
#endregion
#region PUBLIC METHODE
/// <summary>
/// Convertion d'une image en string rtf
/// </summary>
/// <param name="_image">Image a convertir en rtf</param>
/// <param name="rich">Richtextbox de destionation</param>
/// <returns>Image convertie en string rtf</returns>
public static String SrcImageRtf(Image _image, RichTextBox rich)
{
StringBuilder _rtf = new StringBuilder();
_rtf.Append(GetImagePrefix(_image, rich));
_rtf.Append(GetRtfImage(_image, rich));
_rtf.Append(@"}");
return _rtf.ToString();
}
#endregion
}
Conclusion :
l'utilation est tres simple
String rtf = ImageToRtf.SrcImageRtf( votre image , votre richtextbox );
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.