Lekce 7 - Konvert obrázků smajlíků do RTF , Safe Lock - ISIM
Tohle mi taky celkem trvalo, než se mi to podařilo najít a ukrást muhehe. Děkuji tedy zdroji: http://www.codeproject.com/…TextBox-at-R kterému jsem to kradl bezmála hodinu, bže je to fakt velmi moc kód. Doporučuji prohlédnout, abyste věděli, jak je občas těžké krást, to co se dokonce nabízí.
{\pict\wmetafile8\picw396\pich396\picwgoal225\pichgoal225
tenhle
prefix jsem si těžce generoval a už nevím kolik to přesně je ale nějak
16x16 nebo tak, nastavuje se tam pevná šířka. A pak se to pomocí InsertRtf
vloží na dané místo. Tím se zabrání vkládání celé hlavičky a
zničení tak formátu rtf.
/// <summary> /// Převede obrázek do jeho RTF podoby bez RTF hlavičky. Pak se vkládá pomocí Rtb.Insert /// </summary> /// <param name="image">Obrázek (asi bych tam necpal nějaký mega velký, ono je to stejně myslim převede na velikost smajlíků) a inicialozovat je pokud možno na začátku a pak už ne</param> /// <returns>RichTextBox RTF bez hlavičky</returns> public static string GetImageRtf(Image image) { // proč to dělat jednoduše? -> musí se používat clipboard to nejni upe davos friendly /*System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox(); System.Windows.Forms.IDataObject obj = System.Windows.Forms.Clipboard.GetDataObject(); System.Windows.Forms.Clipboard.Clear(); System.Windows.Forms.Clipboard.SetImage(image); rtb.Paste(); System.Windows.Forms.Clipboard.Clear(); System.Windows.Forms.Clipboard.SetDataObject(obj); string imgRtf = rtb.Rtf; imgRtf = imgRtf.Remove(0, imgRtf.IndexOf(@"{\pict")); imgRtf = imgRtf.Replace(@"\par", string.Empty); imgRtf = imgRtf.Remove(imgRtf.LastIndexOf("}")); return imgRtf;*/ // když to jde složitě :D using (System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox()) { rtb.Font = Global.chatFont; StringBuilder _rtf = null; // Used to store the enhanced metafile MemoryStream _stream = null; // Used to create the metafile and draw the image Graphics _graphics = null; // The enhanced metafile Metafile _metaFile = null; // Handle to the device context used to create the metafile IntPtr _hdc; string hexString = ""; try { _rtf = new StringBuilder(); _stream = new MemoryStream(); // Get a graphics context from the RichTextBox using (_graphics = rtb.CreateGraphics()) { // Get the device context from the graphics context _hdc = _graphics.GetHdc(); // Create a new Enhanced Metafile from the device context _metaFile = new Metafile(_stream, _hdc); // Release the device context _graphics.ReleaseHdc(_hdc); } // Get a graphics context from the Enhanced Metafile using (_graphics = Graphics.FromImage(_metaFile)) { // Draw the image on the Enhanced Metafile _graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height)); } // Get the handle of the Enhanced Metafile IntPtr _hEmf = _metaFile.GetHenhmetafile(); // A call to EmfToWmfBits with a null buffer return the size of the // buffer need to store the WMF bits. Use this to get the buffer // size. uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, 8, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault); // Create an array to hold the bits byte[] _buffer = new byte[_bufferSize]; // A call to EmfToWmfBits with a valid buffer copies the bits into the // buffer an returns the number of bits in the WMF. uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, 8, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault); // Append the bits to the RTF string for (int i = 0; i < _buffer.Length; ++i) { _rtf.Append(String.Format("{0:X2}", _buffer[i])); } hexString = _rtf.ToString(); } finally { if (_graphics != null) _graphics.Dispose(); if (_metaFile != null) _metaFile.Dispose(); if (_stream != null) _stream.Close(); } string mpic = @"{\pict\wmetafile8\picw396\pich396\picwgoal225\pichgoal225 "; mpic += hexString; mpic += "}"; return mpic; } } }
Doporučuji také mrknout sem: http://www.itnetwork.cz/…05773f913065 Používám ještě Safe.Lock, který dávám hlavně ve Skype, protože mu nevěřim. Dost často vyprší timeout socketu a on se zasekne při vykonávání metody. Pokud tam zas žádnej lock není tak se můžou spustit dvě metody naráz a to přetíží zase skype wrapper. Tohle zajistí, že po určitém timeoutu pokud metoda není dokončena tak se ukončí její provádění a jde se dál.