Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

7. diel - Konvert obrázkov smajlíkov do RTF, Safe Lock - isim

Toto mi taky celkom trvalo, kým sa mi to podarilo nájsť a ukradnúť muhehe. Ďakujem teda zdrojmi: http://www.codeproject.com/...TextBox-at-R ktorému som to kradol bezmála hodinu, BZE je to fakt veľmi moc kód. Odporúčam pozrieť, aby ste vedeli, ako je občas ťažké kradnúť, to čo sa dokonca ponúka.

{\pict\wmetafile8\picw396\pich396\picwgoal225\pichgoal225 tento prefix som si ťažko generoval a už neviem koľko to presne je ale nejako 16x16 alebo tak, nastavuje sa tam pevná šírka. A potom sa to pomocou InsertRtf vloží na dané miesto. Tým sa zabráni vkladanie celej hlavičky a zničenie tak formáte 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;
        }
    }
}

Odporúčam tiež mrknúť sem: http://www.itnetwork.cz/...05773f913065 Používam ešte Safe.Lock, ktorý dávam hlavne v Skype, pretože mu neverím. Dosť často vyprší timeout socketu a on sa zasekne pri vykonávaní metódy. Ak tam zas žádnej lock nie je tak sa môžu spustiť dve metódy naraz a to preťaží zase skype wrapper. Tohle zaistí, že po určitom timeoutu ak metóda nie je dokončená tak sa ukončí jej vykonávanie a ide sa ďalej.


 

Predchádzajúci článok
Konverter, práca s textom - isim
Všetky články v sekcii
Programujeme vlastné Instant Messenger :: isim ::
Preskočiť článok
(neodporúčame)
Bitové operátory, odpočúvanie klávesov, kreslenie - isim
Článok pre vás napísal David Jančík
Avatar
Užívateľské hodnotenie:
Ešte nikto nehodnotil, buď prvý!
Autor je vášnivý programátor. Nezná slovo "nelze", nebojí se zkoušet nepoznané a pronikat do nových technologií.
Aktivity