将word中的图片保存到本地
As you know The clipboard is a set of functions and messages that enable applications to transfer data. Because all applications have access to the clipboard, data can be easily transferred between applications or within an application.
This article explains how to handle the data in the clipboard and save it into file by using C#
In this article I will use System.Windows.Forms.Clipboard class, this class Provides methods to place data on and retrieve data from the system clipboard and all the method that provided by this class is static method this mean you can use it without create or instantiate an object from this class.
So first of all I Want to explain the main manipulation of the program:
- The method GetDataObject() represent the data that inside the clipboard so we can use it to get data from clipboard or check if there is data in clipboard.
- Because the data type of the object returned from the clipboard can vary, this method returns the data in an IDataObject so I will create an object from IDataObject interface and instantiate it from the value that will coming from GetDataObject()method.
IDataObject data = Clipboard.GetDataObject(); - After that we can use the object data to handle the information that will coming from GetDataObject()or clipboard.
- So we can check the type of this data
if (data.GetDataPresent(DataFormats.Bitmap))
{
} - Or convert it to an appropriate formats like image format
Image image = (Image)data.GetData(DataFormats.Bitmap,true);
Code of article
if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
{
Image image = (Image)data.GetData(DataFormats.Bitmap,true);
image.Save("image.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
image.Save("image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
image.Save("image.gif",System.Drawing.Imaging.ImageFormat.Gif);
}
else
{
MessageBox.Show("The Data In Clipboard is not as image format");
}
}
else
{
MessageBox.Show("The Clipboard was empty");
}
这段代码是把Clipboard中的图片保存到文件中。下面的则是我的讲word中的图片复制到Clipboard中。
range.Paragraphs.Item(i).Range.InlineShapes.Item(1).Range.Copy();