如果单击一个saveas按钮,如何动态地将图像添加到ms字中 [英] how to add image to the ms word dynamically if we click one saveas button

查看:110
本文介绍了如果单击一个saveas按钮,如何动态地将图像添加到ms字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在完成一项任务
我有一个名为"saveas"的按钮
当我单击此按钮时,屏幕上的当前图像必须动态保存在msword中.
在这里,我的图像不过是我的Windows窗体
我动态创建了msword文档,但是我无法将该图像保存到该msword文档中,因此请帮助我,我的代码如下所示:


 私有 无效 sAVEASToolStripMenuItem_Click_1(对象发​​件人,EventArgs e)
        {
            
         //  | JPeg图像| * .jpg |位图图像| * .bmp | Gif图像| * .gif 
             字符串 myStream;
           SaveFileDialog saveFileDialog1 =  SaveFileDialog();
         saveFileDialog1.Filter = " ;
                saveFileDialog1.FilterIndex =  1 ;
                saveFileDialog1.Title = " ;
                saveFileDialog1.RestoreDirectory =  true ;
                saveFileDialog1.AddExtension =  true ;
                saveFileDialog1.CheckPathExists =  true ;
                图形g1 =  .CreateGraphics();
                图片MyImage = 位图( .ClientRectangle.Width, this  .ClientRectangle.Height,g1);
                图形g2 = Graphics.FromImage(MyImage);
                 IntPtr  dc1 = g1.GetHdc();
                 IntPtr  dc2 = g2.GetHdc();
                BitBlt(dc2, 0  0  .ClientRectangle.Width,.ClientRectangle.Height,dc1, 0  13369376 );
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);
                如果(saveFileDialog1.ShowDialog()== DialogResult.OK)
                {
                    // 如果((myStream = saveFileDialog1.OpenFile())!= null)
                    //  {
                    //  StreamWriter wText = new StreamWriter(myStream); 
                    //  wText.Write("saveFileDialog1.OpenFile()"); 
                    //  myStream.Close(); 
                    // } 
                    myStream = saveFileDialog1.FileName;
                    如果(saveFileDialog1.FileName!= " )
                    {
                        System.IO.Stream fileStream =(System.IO.FileStream)saveFileDialog1.OpenFile();
                        MyImage.Save(fileStream,ImageFormat.Jpeg);
                        fileStream.Close();
                        对象 oMissing = System.Reflection.Missing.Value;
                        对象 oEndOfDoc = "  ; /*   \ endofdoc是预定义的书签*/
                        // 启动Word并创建一个新文档.
                        Word._Application oWord;
                        Word._Document oDoc;
                        oWord =  Word.Application();
                        oWord.Visible =  true ;
                        oDoc = oWord.Documents.Add( ref  oMissing, ref  oMissing,
                             ref  oMissing, ref  oMissing);
                        //  Word.InlineShape oShape; 
                        // 对象oClassType ="MSGraph.Chart.8"; 
                        //  wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; 
                        //  oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType,ref oMissing,
                        //  ref oMissing,ref oMissing,ref oMissing,
                        //  ref oMissing,ref oMissing,ref oMissing); 
                       
                    }
                }
}
} 

解决方案

尝试以下站点: object objIndex = 附件"; Microsoft.Office.Interop.Word.Range rangeAttachment = _WordDoc.Bookmarks.get_Item( ref objIndex).Range; 对象 ClassType = objNull; // ClassType:=包装" 对象 FileName = attachmentPathFileName; 对象 LinkToFile = true; 对象 DisplayAsIcon = true; 对象 IconFileName = attachmentPathFileName; 对象 IconIndex = 0; 对象 IconLabel = " // 显示的标签名称,在这里应该是附件的名称"; 对象范围= objNull; rangeAttachment.InlineShapes.AddOLEObject( ref ClassType, ref FileName, ref LinkToFile , ref DisplayAsIcon, ref IconFileName, ref IconIndex, ref IconLabel , ref 范围); _WordDoc.Save();


hi
i am having one task that is
i am having one button named "saveas"
when i click this button the current image on the screen have to be saved in msword dynamically.
here my image is nothing but my windows form
i created msword document dynamically but i am not able to save that image in to that msword document so please help me my code is here like this


private void sAVEASToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            
         //|JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif
             string myStream; 
           SaveFileDialog saveFileDialog1 = new SaveFileDialog();
         saveFileDialog1.Filter = "Word document (*.doc)|*.doc|JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
                saveFileDialog1.FilterIndex = 1; 
                saveFileDialog1.Title = "Save Data File As";
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.AddExtension = true;
                saveFileDialog1.CheckPathExists = true;
                Graphics g1 = this.CreateGraphics();
                Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
                Graphics g2 = Graphics.FromImage(MyImage);
                IntPtr dc1 = g1.GetHdc();
                IntPtr dc2 = g2.GetHdc();
                BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //if ((myStream = saveFileDialog1.OpenFile()) != null)
                    //{
                    //    StreamWriter wText = new StreamWriter(myStream);
                    //    wText.Write(" saveFileDialog1.OpenFile()"); 
                    //    myStream.Close();
                    //}
                    myStream = saveFileDialog1.FileName;
                    if (saveFileDialog1.FileName != "")
                    {
                        System.IO.Stream fileStream = (System.IO.FileStream)saveFileDialog1.OpenFile();
                        MyImage.Save(fileStream, ImageFormat.Jpeg);
                        fileStream.Close();
                        object oMissing = System.Reflection.Missing.Value;
                        object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
                        //Start Word and create a new document.
                        Word._Application oWord;
                        Word._Document oDoc;
                        oWord = new Word.Application();
                        oWord.Visible = true;
                        oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing);
                        //Word.InlineShape oShape;
                        //object oClassType = "MSGraph.Chart.8";
                        //wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                        //oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,
                        //    ref oMissing, ref oMissing, ref oMissing,
                        //    ref oMissing, ref oMissing, ref oMissing);
                       
                    }
                }
}
}

Try this site: Inserting images into Word Using XML[^]


I have not tried this but you could consider these options:

1. Look at VSTO (Visual Studio Tools for Office)
2. Push the image into your clipboard and have Word use an Auto_Open macro that takes the clipboard image to be inserted
3. Google ?

and your English is not that bad.


object objIndex = "附件";
               Microsoft.Office.Interop.Word.Range rangeAttachment = _WordDoc.Bookmarks.get_Item(ref objIndex).Range;

               object ClassType = objNull; // ClassType:="Package"
               object FileName = attachmentPathFileName;
               object LinkToFile = true;
               object DisplayAsIcon = true;
               object IconFileName = attachmentPathFileName;
               object IconIndex = 0;
               object IconLabel = "附件名称【点击查看" // "显示的标签名称,在这里应该是附件的名称";
               object Range = objNull;

               rangeAttachment.InlineShapes.AddOLEObject(
                   ref ClassType, ref FileName, ref LinkToFile, ref DisplayAsIcon,
                   ref IconFileName, ref IconIndex, ref IconLabel, ref Range);

               _WordDoc.Save();


这篇关于如果单击一个saveas按钮,如何动态地将图像添加到ms字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆