How to enable Emacs PNG support on MS Windows

Some might want to enable PNG image support for his/her Emacs on Windows because AUCTeX Preview LaTeX feature or Org LaTeX preview feature may require it. This post assumes you are using official GNU Emacs, and not some bundled versions. (In fact, bundled versions might bundle relevant dlls for you so that you don’t have to do it manually.)

1. How do I check if PNG support is enabled?

If enabled, when you drag and drop a PNG image file to an Emacs frame, the image will be displayed in that buffer. If not enabled, some gibberish text will be shown instead and Emacs will say

Cannot display image: (Invalid image specification)

2. How do I enable it?

As for the latest stable version of GNU Emacs as of this writing, which is GNU Emacs 24.3.1, you only need to copy the library file libpng14-14.dll (and also zlib1.dll) into the Emacs bin folder (the bin folder where emacs.exe is). The files can be found in C:\Program Files\Inkscape (or C:\Program Files (x86)\Inkscape) if you have installed Inkscape, and also in C:\Program Files\GNU\GnuPG if you have installed GNU GnuPG (which I install with the gpg4win installer). Don’t forget to restart Emacs.

In case later versions come out after I write this article and I don’t get to update this, here’s how for later versions of GNU Emacs: first evaluate the following elisp code:

(cdr (assq 'png dynamic-library-alist))

the evaluation result will be something like ("libpng14-14.dll" "libpng14.dll"), then it means that you need to copy the library file libpng14-14.dll or libpng14.dll into your Emacs bin folder.

Another way to see which library files to get is to read a relevant section in GNU Emacs Windows FTP readme.

3. Appendix

3.1. elisp code to check PNG support programmatically

(image-type-available-p 'png)

3.2. things that don’t enable PNG support

According to my test with GNU Emacs 24.3.1, following things don’t successfully enable PNG support.

  1. Adding the path to the folder containing the appropriate libpng dll file to exec-path (Elisp variable) and/or PATH (Windows environment variable).
  2. Copying an older version of libpng dll into Emacs bin. (GnuWin32 ones are likely to be old.)
  3. Changing dynamic-library-alist (or image-library-alist) in the hope that it may make Emacs recognize the old libpng file (in 2).

With 3, error message is something like:

PNG warning: Application was compiled with png.h from libpng-1.4.3
PNG warning: Application  is  running with png.c from libpng-1.2.37
PNG error: Incompatible libpng version in application and library
...
This entry was posted in Emacs and tagged , , . Bookmark the permalink.

5 Responses to How to enable Emacs PNG support on MS Windows

  1. bashee2wang says:

    Very cool solution!

  2. Kristen H. says:

    Thank you for your code! Very useful to know exactly which dll you need. Unfortunately just dropping the dlls into the bin doesn’t seem to work for the April 2015 release. After struggling with it for a bit I went digging into the source code – looks like there’s now an “image-load-path” you can customize, and Emacs will get your dlls from there. The automatic load-path for this is “~\emacs-24.5-bin-i686-mingw32\share\emacs\24.5\etc\images”, as it turns out. They like to make life difficult, don’t they?

    • Kristen H. says:

      Ah, this is what I get for posting w/o triple-checking first – not a solution. Alas.

    • Jisang Yoo says:

      I haven’t tried the new version yet, but it seems the variable image-load-path is not new, as it is also in the old version I am using. I will try to check the dll issues with the new version as soon as I update my Emacs.

      • Kristen H. says:

        I might have just been messing up honestly – my programming background is in R, which is not quite elisp-like. I ended up giving up and installing one of the precompiled binaries with the image libraries added. Dropped in my old .emacs code and I was good. Didn’t work for the spellcheck, unfortunately, but that was fixable.

        Good luck with the update!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s