How to copy a GIS image to the Windows clipboard and paste it to the word

Source: Internet
Author: User

 

How to copy a GIS image to the Windows clipboard and paste it to the word

I haven't written this article for a long time. I want to shut down this blog and stop updating it, but I still have no determination. Let's take a look at these days. Let's send a function that was previously written to play.
The copy and paste functions are simple, but how can we paste GIS images into word? The simplest idea is to save the GIS image as an image, send it to the clipboard, and paste it to word. However, if the image format is in the format of a grid, such as BMP or JPEG, the effect will be affected when the image is pasted and stretched in word, the most suitable format is the WMF or EMF vector format. When you copy the Visio format to word, you can see this effect, and the text can still be edited. Okay. Let's take a look at how to implement this function.

The first step is to copy the image as a Metafile object:

Public Metafile getgeometrymetafile (isymbol psymbol, igeometry pgeometry)
{
// Obtain an idisplaytransformation object first
Idisplaytransformation pdistrans = new displaytransformationclass ();
Ienvelope penv = pgeometry. envelope;
Rectangle rect = new rectangle (0, 0,500,500 );
 
Tagrect R;
R. Left = rect. Left;
R. Right = rect. Right;
R. Bottom = rect. bottom;
R. Top = rect. Top;

Pdistrans. set_deviceframe (ref R );
Pdistrans. bounds = penv;

Pdistrans. Resolution = 72;

Pdistrans. referencescale = 1.0;
Pdistrans. scaleratio = 1;

// Create WMF
Memorystream MS = new memorystream ();
Graphics G = creategraphics ();
Intptr HDC = G. gethdc ();
Metafile mf = new Metafile (MS, HDC, new rectangle (500,500,), metafileframeunit. pixel, emftype. emfplusdual );
G. releasehdc (HDC );
G. Dispose ();
G = graphics. fromimage (MF );

G. fillrectangle (New solidbrush (color. White), new rectangle (0, 0,500,500 ));

Psymbol. setupdc (INT) g. gethdc (), pdistrans );
Psymbol. Draw (pgeometry );
Psymbol. resetdc ();
G. releasehdc ();

G. drawstring ("by Watson", this. Font, new solidbrush (color. Blue), new pointf (20, 20 ));

G. Save ();
G. Dispose ();

Return MF;
}

Step 2: Send to clipboard:

Idataobject DATA = new dataobject ();
Data. setdata (dataformats. metafilepict, MF );
Clipboard. setdataobject (data, true );

Test... what, failed ??
It is estimated that. Net does not support copying the Metafile format to the clipboard.
So I searched for the search engine and found a solution in a Foreign Forum:

Public class clipboardmetafilehelper
{
[Dllimport ("user32.dll")]
Static extern bool openclipboard (intptr hwndnewowner );
[Dllimport ("user32.dll")]
Static extern bool emptyclipboard ();
[Dllimport ("user32.dll")]
Static extern intptr setclipboarddata (uint uformat, intptr hmem );
[Dllimport ("user32.dll")]
Static extern bool closeclipboard ();
[Dllimport ("gdi32.dll")]
Static extern intptr copyenhmetafile (intptr hemfsrc, intptr hnull );
[Dllimport ("gdi32.dll")]
Static extern bool deleteenhmetafile (intptr hemf );

// Metafile mf is set to an invalid state inside this function
Static public bool putenhmetafileonclipboard (intptr hwnd, Metafile MF)
{
Bool bresult = false;
Intptr hemf, hemf2;
Hemf = mf. gethenhmetafile (); // invalidates MF
If (! Hemf. Equals (New intptr (0 )))
{
Hemf2 = copyenhmetafile (hemf, new intptr (0 ));
If (! Hemf2.equals (New intptr (0 )))
{
If (openclipboard (hwnd ))
{
If (emptyclipboard ())
{
Intptr hres = setclipboarddata (14/** // * cf_enhmetafile */, hemf2 );
Bresult = hres. Equals (hemf2 );
Closeclipboard ();
}
}
}
Deleteenhmetafile (hemf );
}
Return bresult;
}
}

You can call the putenhmetafileonclipboard method,
Test again... It's done!

Let's take a look at the code in putenhmetafileonclipboard, especially the copyenhmetafile function, which copies the Metafile in the memory. the Metafile format in. Net differs from that in the Metafile format in windows.

From: http://www.cnblogs.com/watsonyin/archive/2007/11/22/968651.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.