private static void generateThumbnail(string imagePath, string thumbnailPath, int size, ThumbnailClip clip)
 {
     var dir = new DirectoryInfo(Path.GetDirectoryName(thumbnailPath));
     dir.Create(true);
     Bitmap bmp = new Bitmap(imagePath);
     Bitmap resizedBmp = clip == ThumbnailClip.Square ? bmp.GetSquare((int)size) : bmp.GetResized((int)size);
     string thumbnailTempPath = thumbnailPath + ".tmp";
     resizedBmp.Save(thumbnailTempPath, ImageFormat.Png);
     File.Move(thumbnailTempPath, thumbnailPath);
 }
        //
        // GET: /Campaign/

        /// <summary>
        /// Makes an image: campaign background with planet images drawn on it (cheaper than rendering each planet individually)
        /// </summary>
        public Bitmap GenerateGalaxyImage(int campaignID, double zoom = 1, double antiAliasingFactor = 4) {
            zoom *= antiAliasingFactor;
            using (var db = new ZkDataContext()) {
                Campaign camp = db.Campaigns.Single(x => x.CampaignID == campaignID);

                using (Image background = Image.FromFile(Server.MapPath("/img/galaxies/" + camp.MapImageName))) {
                    Bitmap im = new Bitmap((int)(background.Width*zoom), (int)(background.Height*zoom));
                    using (Graphics gr = Graphics.FromImage(im)) {
                        gr.DrawImage(background, 0, 0, im.Width, im.Height);

                        foreach (CampaignPlanet p in camp.CampaignPlanets) {
                            string planetIconPath = null;
                            Resource map = db.Resources.FirstOrDefault(m => m.InternalName == p.Mission.Map);
                            try {
                                planetIconPath = "/img/planets/" + (map.MapPlanetWarsIcon ?? "1.png"); // backup image is 1.png
                                using (Image pi = Image.FromFile(Server.MapPath(planetIconPath))) {
                                    double aspect = pi.Height/(double)pi.Width;
                                    var width = (int)(map.PlanetWarsIconSize * zoom);
                                    var height = (int)(width*aspect);
                                    gr.DrawImage(pi, (int)(p.X*im.Width) - width/2, (int)(p.Y*im.Height) - height/2, width, height);
                                }
                            } catch (Exception ex) {
                                throw new ApplicationException(
                                    string.Format("Cannot process planet image {0} for planet {1} map {2}",
                                                  planetIconPath,
                                                  p.PlanetID,
                                                  map.ResourceID),
                                    ex);
                            }
                        }
                        if (antiAliasingFactor == 1) return im;
                        else {
                            zoom /= antiAliasingFactor;
                            return im.GetResized((int)(background.Width*zoom), (int)(background.Height*zoom), InterpolationMode.HighQualityBicubic);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Makes an image: galaxy background with planet images drawn on it (cheaper than rendering each planet individually)
        /// </summary>
        // FIXME: having issues with bitmap parameters; setting AA factor to 1 as fallback (was 4)
        public Bitmap GenerateGalaxyImage(int galaxyID, double zoom = 1, double antiAliasingFactor = 1)
        {
            zoom *= antiAliasingFactor;
            using (var db = new ZkDataContext())
            {
                Galaxy gal = db.Galaxies.Single(x => x.GalaxyID == galaxyID);

                using (Image background = Image.FromFile(Server.MapPath("/img/galaxies/" + gal.ImageName)))
                {
                    //var im = new Bitmap((int)(background.Width*zoom), (int)(background.Height*zoom));
                    var im = new Bitmap(background.Width, background.Height);
                    using (Graphics gr = Graphics.FromImage(im))
                    {
                        gr.DrawImage(background, 0, 0, im.Width, im.Height);

                        /*
						using (var pen = new Pen(Color.FromArgb(255, 180, 180, 180), (int)(1*zoom)))
						{
							foreach (var l in gal.Links)
							{
								gr.DrawLine(pen,
								            (int)(l.PlanetByPlanetID1.X*im.Width),
								            (int)(l.PlanetByPlanetID1.Y*im.Height),
								            (int)(l.PlanetByPlanetID2.X*im.Width),
								            (int)(l.PlanetByPlanetID2.Y*im.Height));
							}
						}*/

                        foreach (Planet p in gal.Planets)
                        {
                            string planetIconPath = null;
                            try
                            {
                                planetIconPath = "/img/planets/" + (p.Resource.MapPlanetWarsIcon ?? "1.png"); // backup image is 1.png
                                using (Image pi = Image.FromFile(Server.MapPath(planetIconPath)))
                                {
                                    double aspect = pi.Height / (double)pi.Width;
                                    var width = (int)(p.Resource.PlanetWarsIconSize * zoom);
                                    var height = (int)(width * aspect);
                                    gr.DrawImage(pi, (int)(p.X * im.Width) - width / 2, (int)(p.Y * im.Height) - height / 2, width, height);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new ApplicationException(
                                    string.Format("Cannot process planet image {0} for planet {1} map {2}",
                                                  planetIconPath,
                                                  p.PlanetID,
                                                  p.MapResourceID),
                                    ex);
                            }
                        }
                        if (antiAliasingFactor == 1) return im;
                        else
                        {
                            zoom /= antiAliasingFactor;
                            return im.GetResized((int)(background.Width * zoom), (int)(background.Height * zoom), InterpolationMode.HighQualityBicubic);
                        }
                    }
                }
            }
        }
 private static void generatePhotoFile(string imagePath, string resizedPath, PhotoFileSize size,
     PhotoFileClip clip)
 {
     var dir = new DirectoryInfo(Path.GetDirectoryName(resizedPath));
     dir.Create(true);
     using (Bitmap bmp = new Bitmap(imagePath))
     {
         bmp.AutoRotate();
         using (BitmapEx resizedBmp = clip == PhotoFileClip.Square ? bmp.GetSquare((int)size) : bmp.GetResized((int)size))
         {
             string thumbnailTempPath = resizedPath + ".tmp";
             var encodeParams = new EncoderParameters(1);
             encodeParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, instance.qualityLevel);
             resizedBmp.Bitmap.Save(thumbnailTempPath, jgpEncoder, encodeParams);
             File.Move(thumbnailTempPath, resizedPath);
         }
     }
 }
        private static void saveCoverArt(string artist, string albumTitle, string sourceUrl, string destination)
        {
            if (sourceUrl != null)
            {
                string destinationDirectory = Directory.GetParent(destination).FullName;
                // if the destination directory doesn't exist
                if( !Directory.Exists( destinationDirectory ))
                {
                    Directory.CreateDirectory(destinationDirectory);
                }
                WebClient client = new WebClient();
                Stream imgStream;
                try
                {
                    imgStream = client.OpenRead(sourceUrl);
                }
                catch (WebException ex)
                {
                    if (log.IsErrorEnabled)
                        log.Error(ex.Message, ex.InnerException);
                    return;
                }

                using (Bitmap bmp = new Bitmap(System.Drawing.Image.FromStream(imgStream)))
                {
                    using (BitmapEx resizedBmp = bmp.GetResized(imageSize))
                    {
                        resizedBmp.Bitmap.Save(destination, ImageFormat.Jpeg);
                    }
                }
            }
        }