How to convert formatted text into images

Source: Internet
Author: User

I have been searching for materials in the blog garden, but I haven't written a blog for a long time. Today, I need to convert text into images. I haven't found a good solution on the Internet for a long time. I finally thought of a good note, recorded it, and shared it with you.

Problem: When we need to display text files elsewhere, it may be easy, but if we want to ensure that the format is the same, it may become a very difficult task. For example, a piece of text that was originally edited in word or Excel is displayed on a webpage and requires typographical layout, font size, and table... Just like the original one, it will become very difficult.

Solution: Convert the edited text into an image.

But how can we change the text into an image, while the original style remains unchanged? The text is not hard, but the difficulty lies in retaining the style.

Solution: I found that copying text in Word is available in [drawing] (Windows attachments ).Program. That is to say, the [drawing] program already has this function, as long as it can be called.

In this way, the solution becomes: put the text in Word ---> copy in Word ---> paste in [drawing] ---> copy in [drawing] ---> then retrieve the image from the clipboard.

Word is relatively easy to control, but the [drawing] program does not have a ready-made interface, it is more difficult to control, you need to use Windows API functions, send messages to the [drawing] program for control purposes.

Written in C #CodeFor more information, see the following:

[Dllimport ("user32.dll", entrypoint = "findwindow", setlasterror = true)] Private Static extern intptr findwindow (string lpclassname, string lpwindowname); [dllimport ("user32.dll ", entrypoint = "find1_wex", setlasterror = true)] Private Static extern intptr find1_wex (intptr hwndparent, uint parent, string lpszclass, string lpszwindow); [dllimport ("user32.dll ", entrypoint = "sendmessag E ", setlasterror = true, charset = charset. auto)] Private Static extern int sendmessage (intptr hwnd, uint wmsg, int wparam, int lparam); [dllimport ("user32.dll", entrypoint = "setforegroundwindow", setlasterror = true)] private Static extern void setforegroundwindow (intptr hwnd); [dllimport ("user32.dll", entrypoint = "getmenu")] public static extern int getmenu (INT hwnd ); [dllimport ("user32.dll ", Entrypoint = "getsubmenu")] public static extern int getsubmenu (INT hmenu, int NPOs); [dllimport ("user32.dll", entrypoint = "getmenuitemid")] public static extern int getmenuitemid (INT hmenu, int NPOs); [dllimport ("user32.dll", entrypoint = "postmessage")] public static extern int postmessage (INT hwnd, int wmsg, int wparam, int lparam); Private const int wm_command = 0x111; private image pasteimage () {// Let only one painting program run in the background, multiple kills, open if not, // The Name Of The drawing program in the process is mspaint, no suffix EXE system. diagnostics. process [] process = system. diagnostics. process. getprocessesbyname (@ "mspaint"); If (process. count ()! = 1) {foreach (system. diagnostics. process P in process) {P. kill ();} processstartinfo startinfo = new processstartinfo (); startinfo. filename = @ "mspaint.exe"; startinfo. createnowindow = false; startinfo. windowstyle = processwindowstyle. hidden; system. diagnostics. process TXT = process. start (startinfo); // wait for a while and get a sleep thread. sleep (1000);} // find the <drawing> handle // The New <drawing> window name is "untitled-drawing", with a space in the middle intptr hwndcalc = fin Dwindow (null, "untitled-drawing"); If (hwndcalc! = Intptr. zero) {// get the menu int GM = getmenu (hwndcalc. toint32 (); GM = getsubmenu (GM, 1); // paste int id = getmenuitemid (GM, 5); postmessage (hwndcalc. toint32 (), wm_command, ID, 0); // copy id = getmenuitemid (GM, 4); postmessage (hwndcalc. toint32 (), wm_command, ID, 0);} // sleep for a while. Otherwise, the system may not be ready yet and the thread will be taken from the clipboard. sleep (1000); idataobject DATA = system. windows. forms. clipboard. getdataobject (); Return (image) data. getdata (typeof (Bitmap ));}

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.