Click here to Skip to main content
15,891,633 members
Articles / Web Development / HTML

Gallery Server Pro - An ASP.NET Gallery for Sharing Photos, Video, Audio and Other Media

Rate me:
Please Sign up or sign in to vote.
4.86/5 (131 votes)
18 Oct 2013GPL331 min read 827.7K   539  
Gallery Server Pro is a complete, stable ASP.NET gallery for sharing photos, video, audio and other media. This article presents the overall architecture and major features.
using System;
using System.Diagnostics;
using System.Text;
using GalleryServerPro.ErrorHandler.CustomExceptions;

namespace GalleryServerPro.Business
{
	/// <summary>
	/// Contains functionality for interacting with ImageMagick, the open source utility. Specifically, Gallery Server Pro uses it to generate
	/// thumbnail images for .eps and .pdf files. See http://www.imagemagick.org for more information.
	/// </summary>
	/// <remarks>Requires Visual C++ 2008 Redistributable Package (x86). 64-bit OS requires both x86 and x64 versions. These are installed with
	/// .NET 3.5 but I am not sure about .NET 4.0.
	/// To read .eps and .pdf files, GhostScript must be installed on the server.</remarks>
	public class ImageMagick
	{
		private StringBuilder _sbOutput;
		private StringBuilder _sbError;

		/// <summary>
		/// Generates a thumbnail image for the media file at the specified <paramref name="mediaFilePath" /> and returns the output from the
		/// execution of the convert utility. The thumbnail is saved to 
		/// <paramref name="thumbnailFilePath" />. The <paramref name="galleryId" /> is used during error handling to associate the error,
		/// if any, with the gallery. Requires the application to be running at Full Trust and GhostScript to be installed on the server. Returns <see cref="String.Empty" /> when the 
		/// application is running at less than Full Trust or when the convert utility is not present in the bin directory.
		/// </summary>
		/// <param name="mediaFilePath">The full file path to the source media file. Example: D:\media\myfile.eps</param>
		/// <param name="thumbnailFilePath">The full file path to store the thumbnail image to. If a file with this name is already present,
		/// it is overwritten.</param>
		/// <param name="galleryId">The gallery ID.</param>
		/// <returns>Returns the text output from the execution of the convert.exe utility.</returns>
		public static string GenerateThumbnail(string mediaFilePath, string thumbnailFilePath, int galleryId)
		{
			string convertOutput = String.Empty;

			if ((AppSetting.Instance.AppTrustLevel != ApplicationTrustLevel.Full) || (String.IsNullOrEmpty(AppSetting.Instance.ImageMagickConvertPath)))
			{
				return convertOutput;
			}

			// Create arguments. The [0] tells it to generate one image from the first page for PDF files (otherwise we get one image for every page)
			// Example: "D:\media\pic.eps[0]" "D:\media\pic.jpg"
			string args = string.Format(@"""{0}[0]"" ""{1}""", mediaFilePath, thumbnailFilePath);

			ImageMagick imageMagick = new ImageMagick();
			convertOutput = imageMagick.ExecuteConvert(args, galleryId);

			if (!String.IsNullOrEmpty(convertOutput))
			{
				// The utility returns an empty string when it is successful, so something went wrong. Log it.
				ErrorHandler.Error.Record(new BusinessException(convertOutput), galleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
			}

			return convertOutput;
		}

		/// <summary>
		/// Execute the ImageMagick convert.exe utility with the given <paramref name="arguments" /> and return the text output generated by it.
		/// See http://www.imagemagick.org for documentation.
		/// </summary>
		/// <param name="arguments">The argument values to pass to the ImageMagick convert.exe utility. 
		/// Example: -density 300 "D:\media\myimage.eps[0]" "D:\media\zThumb_myimage.jpg"</param>
		/// <param name="galleryId">The gallery ID.</param>
		/// <returns>Returns the text output from the execution of the convert.exe utility.</returns>
		private string ExecuteConvert(string arguments, int galleryId)
		{
			ProcessStartInfo info = new ProcessStartInfo(AppSetting.Instance.ImageMagickConvertPath, arguments);
			info.UseShellExecute = false;
			info.CreateNoWindow = true;
			info.RedirectStandardError = true;
			info.RedirectStandardInput = true;
			info.RedirectStandardOutput = true;

			using (Process p = new Process())
			{
				try
				{
					_sbOutput = new StringBuilder();
					_sbError = new StringBuilder();

					p.StartInfo = info;
					p.OutputDataReceived += new DataReceivedEventHandler(Convert__OutputDataReceived);
					p.ErrorDataReceived += new DataReceivedEventHandler(Convert_ErrorDataReceived);

					p.Start();

					p.BeginOutputReadLine();
					p.BeginErrorReadLine();

					p.WaitForExit();
					p.Close();
				}
				catch (Exception ex)
				{
					ErrorHandler.Error.Record(ex, galleryId, Factory.LoadGallerySettings(), AppSetting.Instance);
				}
			}

			// If we have error data, append it to the output string.
			if (_sbError.Length > 0)
			{
				_sbOutput.Append(_sbError.ToString());
			}

			// Return the output from the utility.
			return _sbOutput.ToString();
		}

		void Convert_ErrorDataReceived(object sender, DataReceivedEventArgs e)
		{
			if (!String.IsNullOrEmpty(e.Data))
			{
				_sbError.AppendLine(e.Data);
			}
		}

		void Convert__OutputDataReceived(object sender, DataReceivedEventArgs e)
		{
			if (!String.IsNullOrEmpty(e.Data))
			{
				_sbOutput.AppendLine(e.Data);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Tech Info Systems
United States United States
I have nearly 20 years of industry experience in software development, architecture, and Microsoft Office products. My company Tech Info Systems provides custom software development services for corporations, governments, and other organizations. Tech Info Systems is a registered member of the Microsoft Partner Program and I am a Microsoft Certified Professional Developer (MCPD).

I am the creator and lead developer of Gallery Server Pro, a free, open source ASP.NET gallery for sharing photos, video, audio, documents, and other files over the web. It has been developed over several years and has involved thousands of hours. The end result is a robust, configurable, and professional grade gallery that can be integrated into your web site, whether you are a large corporation, small business, professional photographer, or a local church.

Comments and Discussions