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 (?font color="#FF0000" face="Courier New">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");
}
以前对于XML如何存储数据很感兴趣,特别是看到一个文章说,可以直接用XML代替SQL server2000用来存储数据和交互数据,目前还在研究之中。终于把如何存取图像到xml的代码搞定:)
读取图像
//定义图像源与目标xml文件
string ImgFileName = @"d:/中国移动暴强广告.JPG";
string XmlFileName = @"D:/img.xml";
XmlTextWriter aXmlTextWriter = new XmlTextWriter(XmlFileName, System.Text.Encoding.Default);
aXmlTextWriter.Formatting = Formatting.Indented;
try
{
aXmlTextWriter.WriteStartDocument();
aXmlTextWriter.WriteComment("Contains a BinHex JPEG image");
aXmlTextWriter.WriteStartElement("jpeg");
//下边就是通用的读取图像的代码
System.IO.FileInfo fi = new System.IO.FileInfo(ImgFileName);
int size = (int)fi.Length;
//read the jpeg file
byte []img = new byte[size];
System.IO.FileStream fs = new System.IO.FileStream(ImgFileName, System.IO.FileMode.Open);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
img = br.ReadBytes(size);
br.Close();
//注意这里用的是BinHex编码
aXmlTextWriter.WriteBinHex(img,0,size);
aXmlTextWriter.WriteEndDocument();
}
catch(XmlException xmlE)
{
Response.Write(xmlE.Message);
}
finally
{
aXmlTextWriter.Close();
}
显示图像
简单的在窗口中放一个PictureBox,在一个按钮中写如下代码
string XmlFileName = @"D:/img.xml";
XmlTextReader aXmlTextReader = new XmlTextReader(XmlFileName);
aXmlTextReader.Read();
aXmlTextReader.MoveToContent();
if(aXmlTextReader.LocalName == "jpeg")
{
System.IO.FileInfo fi = new System.IO.FileInfo(XmlFileName);
int iSize = (int)fi.Length;
byte []img = new byte[iSize];
aXmlTextReader.ReadBinHex(img,0,iSize);
//Byte to image object
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(img,0,iSize);
Bitmap bmp = new Bitmap(ms);
ms.Close();
this.pictureBox1.Image = bmp;
}
aXmlTextReader.Close();