总体目标:把word转化为图片,显示在网页中。
思路:word全选,复制,选择性粘贴为图片,下面是我的作法,:
object Nothing = System.Reflection.Missing.Value; object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application WordApp =Marshal.GetActiveObject( "Word.Application" ) as Microsoft.Office.Interop.Word.ApplicationClass ; Microsoft.Office.Interop.Word.Document WordDoc = WordApp.ActiveDocument; WordDoc.Select(); WordDoc.ActiveWindow.Selection.CopyAsPicture(); WordDoc.ActiveWindow.Selection.Delete(ref missing,ref missing) ; object type = Microsoft.Office.Interop.Word.WdPasteDataType.wdPasteEnhancedMetafile; WordDoc.ActiveWindow.Selection.PasteSpecial(ref missing, ref missing, ref missing, ref missing, ref type, ref missing, ref missing);
我的问题:结果只得到的是矢量图,但我需要的是位图。
即得到的是选中时这个周边带圆圈的
图片,是个矢量图。
在word中通过选择性粘贴,可以得到增强图元文件,,就是选中时显示黑点边框的图片。这正是我想要的,怎么做,请求帮助?
另外,我用过【画图】程序实现过(http://www.cnblogs.com/eyye/archive/2012/04/24/2467397.html),但有两个两个问题没法解决(1)有的电脑真没【画图程序】,(2)最重要的是转换结果不是很满意,有些线条会丢失的。所以我希望在word内部实现,当然是用C#程序控制。
private Bitmap CopyFromWordToImage()
{
Bitmap b = null;
object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application WordApp = Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.ApplicationClass;
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.ActiveDocument;
WordDoc.ActiveWindow.Selection.WholeStory();
WordDoc.ActiveWindow.Selection.Copy();
IntPtr hwnd = ClsWindows.FindWindow(null, WordDoc.Name + " - Microsoft Word");
try
{
if (ClsWindows.OpenClipboard(hwnd))
{
IntPtr data = ClsWindows.GetClipboardData(14);
if (data != IntPtr.Zero)
{
using (Metafile mf = new Metafile(data, true))
{
int w = mf.Width;
int h = mf.Height;
int maxh = 1000;
float r = (float)(w * 1.0 / h);
if (h > maxh)
{
h = maxh;
w = (int)(h * r);
}
int maxw = 600;
if (w > maxw)
{
w = maxw;
h= (int)(w / r);
}
b = new Bitmap( w, h);
Graphics g =Graphics.FromImage(b) ;
g.Clear(Color.White);
g.DrawImage(mf, 0, 0);
b.Save(@"D:\web\" + System.Guid.NewGuid() + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg ); ;
}
}
}
}
finally { ClsWindows.CloseClipboard(); }
return b;
}