示例#1
0
        private string InitImage(string id)
        {
            Guid        retValue;
            var         rel  = _docRels.Where(c => c.Id == id).First();
            string      ext  = new FileInfo(rel.TargetUri.ToString()).Extension.ToLower();
            PackagePart part = _package.GetPart(new Uri("/word/" + rel.TargetUri.ToString(), UriKind.Relative));

            using (Stream partStream = part.GetStream())
            {
                Image img = null;
                if (ext == ".wmf" || ext == ".emf")
                {
                    img = new Metafile(partStream);
                }
                else
                {
                    img = Bitmap.FromStream(partStream);
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, ImageFormat.Png);
                    retValue = _currentQuestion.AddImage(ms.ToArray());
                }
            }
            return(retValue.ToString());
        }
示例#2
0
        private void ConvertRtf()
        {
            int    count = 0;
            string st    = String.Empty;

            for (int i = 0; i < _rtfCom.Lines.Length; i++)
            {
                for (int j = 0; j < _rtfCom.Lines[i].Length + 1; j++)
                {
                    _rtfCom.Select(count, 1);
                    if (ENABLE_TABLES)
                    {
                        st += TableProcess(_rtfCom.SelectionTabs.Length > 0);
                    }
                    if (_isTable && _rtfCom.SelectedText.Contains("\t"))
                    {
                        if (ENABLE_TABLES && _rtfCom.SelectedText.Contains("\n"))
                        {
                            st += "</tr>";
                            _rtfCom.Select(count + 1, 1);
                            if (_rtfCom.SelectionTabs.Length > 0)
                            {
                                st += "<tr><td>";
                            }
                            _rtfCom.Select(count, 1);
                        }
                        else
                        {
                            _rtfCom.Select(count + 1, 1);
                            if (_rtfCom.SelectionType != (RichTextBoxSelectionTypes.Text
                                                          | RichTextBoxSelectionTypes.MultiChar) &&
                                _rtfCom.SelectionType != (RichTextBoxSelectionTypes.Text
                                                          | RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.Object) &&
                                _rtfCom.SelectionType != (RichTextBoxSelectionTypes.Text | RichTextBoxSelectionTypes.Object
                                                          | RichTextBoxSelectionTypes.MultiChar | RichTextBoxSelectionTypes.MultiObject))
                            {
                                CloseTags(ref st);
                                st += "</td><td>";
                            }
                            _rtfCom.Select(count, 1);
                        }
                    }
                    if (_rtfCom.SelectionType == RichTextBoxSelectionTypes.Text)
                    {
                        st += BoldProcess(_rtfCom.SelectionFont.Bold);
                        st += ItalicProcess(_rtfCom.SelectionFont.Italic);
                        st += UnderlineProcess(_rtfCom.SelectionFont.Underline);
                        st += CharOffsetProcess(_rtfCom.SelectionCharOffset);
                        st += BulletProcess(_rtfCom.SelectionBullet);
                        if (_rtfCom.SelectedRtf.IndexOf("\\super") > 0)
                        {
                            st += CharOffsetProcess(3);
                        }
                        if (_rtfCom.SelectedRtf.IndexOf("\\sub") > 0)
                        {
                            st += CharOffsetProcess(-3);
                        }
                        st += ColorProcess(_rtfCom.SelectionColor);
                        if (_rtfCom.SelectedText == "\n")
                        {
                            if (_isBullet)
                            {
                                _rtfCom.Select(count + 1, 1);
                                if (_rtfCom.SelectionBullet)
                                {
                                    st += "<li>";
                                }
                                _rtfCom.Select(count, 1);
                            }
                            else
                            {
                                st += "<br/>";
                            }
                        }
                        else
                        {
                            char[] ch = _rtfCom.SelectedText.ToCharArray();
                            foreach (char c in ch)
                            {
                                string st1 = String.Format("&#{0};", ((UInt16)c).ToString(CultureInfo.InvariantCulture));
                                if ((UInt16)c > 1104)
                                {
                                    st += st1;
                                }
                                else
                                {
                                    st += HttpUtility.HtmlEncode(_rtfCom.SelectedText);
                                }
                            }
                        }
                    }
                    else if (_rtfCom.SelectionType == RichTextBoxSelectionTypes.Object)
                    {
                        _rtfCom.Copy();
                        byte[] pngImage = null;
                        if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Bitmap))
                        {
                            Bitmap bitmap = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
                            using (MemoryStream ms = new MemoryStream())
                            {
                                bitmap.Save(ms, ImageFormat.Png);
                                pngImage = ms.ToArray();
                                ms.Close();
                            }
                        }
                        else
                        {
                            if (ClipboardAPI.OpenClipboard(_rtfCom.Handle))
                            {
                                if (ClipboardAPI.IsClipboardFormatAvailable(ClipboardAPI.CF_ENHMETAFILE))
                                {
                                    IntPtr   henmetafile = ClipboardAPI.GetClipboardData(ClipboardAPI.CF_ENHMETAFILE);
                                    Metafile metaFile    = new Metafile(henmetafile, true);
                                    ClipboardAPI.CloseClipboard();
                                    int w = FindSize(_rtfCom.SelectedRtf, "picwgoal");
                                    int h = FindSize(_rtfCom.SelectedRtf, "pichgoal");

                                    pngImage = RtfImage.GetMetafile(metaFile, w, h);

                                    Marshal.Release(henmetafile);
                                }
                            }
                        }
                        if (pngImage == null)
                        {
                            count++;
                            continue;
                        }
                        Guid unid = _htmlStore.AddImage(pngImage);
                        st = String.Format("{0}<img src=\"#${1}.png\"/>", st, unid.ToString());
                    }
                    else if (_rtfCom.SelectionType == RichTextBoxSelectionTypes.Object)
                    {
                        byte[] pngImage = RtfImage.GetBitmap(_rtfCom.SelectedRtf);
                        if (pngImage == null)
                        {
                            count++;
                            continue;
                        }
                        Guid unid = _htmlStore.AddImage(pngImage);
                        st = String.Format("{0}<img src=\"#${1}.png\"/>", st, unid.ToString());
                    }

                    count++;
                }
            }
            CloseTags(ref st);
            _htmlStore.Html = st;
        }