Ejemplo n.º 1
0
        /// <summary>
        /// Creates an embedded shadow copy of a source image file.
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="fileService"></param>
        private void CreateShadowFile(ImageFileData sourceFile, ISupportingFileService fileService)
        {
            Size shadowSize = new Size(1280, 960);

            using (MemoryStream shadowStream = new MemoryStream())
            {
                ImageFormat format;
                string      fileExt;
                ImageHelper2.GetImageFormat(sourceFile.SupportingFile.FileName, out fileExt, out format);
                using (Bitmap sourceImage = new Bitmap(sourceFile.Uri.LocalPath))
                {
                    if (sourceImage.Width > shadowSize.Width || sourceImage.Height > shadowSize.Height)
                    {
                        shadowSize = ImageHelper2.SaveScaledThumbnailImage(Math.Min(shadowSize.Width, sourceImage.Width),
                                                                           Math.Min(shadowSize.Height, sourceImage.Height),
                                                                           sourceImage, format, shadowStream);
                    }
                    else
                    {
                        shadowSize = sourceImage.Size;
                        using (FileStream fs = File.OpenRead(sourceFile.Uri.LocalPath))
                        {
                            StreamHelper.Transfer(fs, shadowStream);
                        }
                    }
                }
                shadowStream.Seek(0, SeekOrigin.Begin);

                ISupportingFile supportingFile = fileService.CreateSupportingFile(sourceFile.SupportingFile.FileName, shadowStream);
                _imageSourceShadowFile = new ImageFileData(supportingFile, shadowSize.Width, shadowSize.Height, ImageFileRelationship.SourceShadow);
            }
        }
Ejemplo n.º 2
0
 public static Size WriteImageToFile(string sourceFile, int width, int height, string outputFile, bool preserveConstraints)
 {
     using (Bitmap sourceImage = new Bitmap(sourceFile))
     {
         FixImageOrientation(sourceImage);
         using (Stream imageOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
         {
             ImageFormat format;
             string      fileExt;
             ImageHelper2.GetImageFormat(sourceFile, out fileExt, out format);
             if (preserveConstraints)
             {
                 ImageHelper2.SaveScaledThumbnailImage(width, height, sourceImage, format, imageOut);
             }
             else
             {
                 ImageHelper2.SaveThumbnailImage(width, height, sourceImage, format, imageOut);
             }
         }
     }
     if (preserveConstraints)
     {
         using (Bitmap img = new Bitmap(outputFile))
             return(img.Size);
     }
     else
     {
         return(new Size(width, height));
     }
 }
Ejemplo n.º 3
0
        private string writeImage(Bitmap image, string srcFileName, string suffix, ImageFilter filter, CreateFileCallback createFileCallback)
        {
            string extension = Path.GetExtension(srcFileName).ToLower(CultureInfo.InvariantCulture);

            srcFileName = Path.GetFileNameWithoutExtension(srcFileName) + suffix + extension;
            try
            {
                //save the thumbnail to disk
                ImageFormat imageFormat;
                ImageHelper2.GetImageFormat(srcFileName, out extension, out imageFormat);
                string filename = createFileCallback(Path.GetFileNameWithoutExtension(srcFileName) + extension);
                try
                {
                    if (filter != null)
                    {
                        image = filter(image);
                    }

                    using (FileStream fs = new FileStream(filename, FileMode.Create))
                    {
                        ImageHelper2.SaveImage(image, imageFormat, fs);
                    }
                    return(filename);
                }
                catch (Exception e)
                {
                    Trace.Fail("Failed to save image as format " + imageFormat.Guid + ": " + e.ToString());

                    //try to fall back to generating a PNG version of the image
                    imageFormat = ImageFormat.Png;
                    extension   = ".png";
                    filename    = createFileCallback(Path.GetFileNameWithoutExtension(srcFileName) + extension);

                    using (FileStream fs = new FileStream(filename, FileMode.Create))
                    {
                        ImageHelper2.SaveImage(image, ImageFormat.Png, fs);
                    }
                    return(filename);
                }
            }
            catch (Exception e)
            {
                Trace.Fail("Error while trying to create thumbnail: " + e.Message, e.StackTrace);
                throw;
            }
        }
Ejemplo n.º 4
0
        private void SetPreviewPicture(Bitmap image, string filename)
        {
            int    maxWidth      = _previewBox.Width - 2;
            int    maxHeight     = _previewBox.Height - 2;
            bool   scaled        = true;
            int    currentWidth  = image.Width;
            int    currentHeight = image.Height;
            double ratio         = 1.00;

            //if height and width are too big
            if (currentWidth > maxWidth && currentHeight > maxHeight)
            {
                //size according to the one that is more off of the available area
                if ((maxWidth / currentWidth) < (maxHeight / currentHeight))
                {
                    ratio = (double)maxWidth / (double)currentWidth;
                }
                else
                {
                    ratio = (double)maxHeight / (double)currentHeight;
                }
            }
            //if just width
            else if (currentWidth > maxWidth)
            {
                ratio = (double)maxWidth / (double)currentWidth;
            }
            //if just height
            else if (currentHeight > maxHeight)
            {
                ratio = (double)maxHeight / (double)currentHeight;
            }
            //else fine
            else
            {
                scaled = false;
            }

            ImageFormat format;
            string      fileExt;

            ImageHelper2.GetImageFormat(filename, out fileExt, out format);
            int newWidth  = (int)(currentWidth * ratio);
            int newHeight = (int)(currentHeight * ratio);

            Bitmap newImage = ImageHelper2.CreateResizedBitmap(image, newWidth, newHeight, format);

            //if image is small enough, add border
            if (newWidth <= maxWidth && newHeight <= maxHeight)
            {
                Bitmap borderPic = new Bitmap(newWidth + 2, newHeight + 2);
                //Get a graphics object for it
                using (Graphics g = Graphics.FromImage(borderPic))
                {
                    g.TextRenderingHint = TextRenderingHint.AntiAlias;

                    int   R    = SystemColors.Control.R;
                    int   G    = SystemColors.Control.G;
                    int   B    = SystemColors.Control.B;
                    Color dark = Color.FromArgb((int)(R * 0.9), (int)(G * 0.9), (int)(B * 0.9));
                    //draw the border
                    g.FillRectangle(new SolidBrush(dark), new Rectangle(0, 0, borderPic.Width, 1));
                    g.FillRectangle(new SolidBrush(dark), new Rectangle(0, borderPic.Height - 1, borderPic.Width, 1));
                    g.FillRectangle(new SolidBrush(dark), new Rectangle(0, 0, 1, borderPic.Height));
                    g.FillRectangle(new SolidBrush(dark), new Rectangle(borderPic.Width - 1, 0, 1, borderPic.Height));

                    //draw our image back in the middle
                    g.DrawImage(newImage, new Rectangle(1, 1, newWidth, newHeight), 0, 0, newWidth, newHeight, GraphicsUnit.Pixel);
                }
                _previewBox.Image = borderPic;
            }
            else
            {
                _previewBox.Image = newImage;
            }

            string picsize;

            if (!scaled)
            {
                picsize = MakeDimensions(currentWidth, currentHeight);
            }
            else
            {
                picsize = string.Format(CultureInfo.CurrentCulture, Res.Get(StringId.InsertImageDimensionsFormatScaled),
                                        MakeDimensions(currentWidth, currentHeight));
            }
            _fileSize.Text = picsize;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes and registers images with the editor.
        /// </summary>
        protected override void DoWork()
        {
            foreach (NewImageInfo newImage in this.newImages)
            {
                // Before starting on the next image, make sure we weren't cancelled.
                if (CancelRequested)
                {
                    AcknowledgeCancel();
                    return;
                }

                try
                {
                    // Register the image file as a BlogPostImageData so that the editor can manage the supporting files
                    ISupportingFile sourceFile = this.fileService.AddLinkedSupportingFileReference(newImage.ImageInfo.ImageSourceUri);
                    newImage.ImageData = new BlogPostImageData(new ImageFileData(sourceFile, newImage.ImageInfo.ImageSourceSize.Width, newImage.ImageInfo.ImageSourceSize.Height, ImageFileRelationship.Source));

                    // Create the shadow file if necessary.
                    if (GlobalEditorOptions.SupportsFeature(ContentEditorFeature.ShadowImageForDrafts))
                    {
                        newImage.ImageData.InitShadowFile(this.fileService);
                    }

                    // Create the initial inline image.
                    Stream inlineImageStream = new MemoryStream();
                    using (Bitmap sourceBitmap = new Bitmap(newImage.ImageInfo.ImageSourceUri.LocalPath))
                    {
                        string      extension;
                        ImageFormat imageFormat;
                        ImageHelper2.GetImageFormat(newImage.ImageInfo.ImageSourceUri.LocalPath, out extension, out imageFormat);

                        using (Bitmap resizedBitmap = ImageHelper2.CreateResizedBitmap(sourceBitmap, newImage.InitialSize.Width, newImage.InitialSize.Height, imageFormat))
                        {
                            // Resizing the bitmap is a time-consuming operation, so its possible we were cancelled during it.
                            if (CancelRequested)
                            {
                                AcknowledgeCancel();
                                return;
                            }

                            ImageHelper2.SaveImage(resizedBitmap, imageFormat, inlineImageStream);
                        }

                        inlineImageStream.Seek(0, SeekOrigin.Begin);
                    }

                    // Saving the bitmap is a time-consuming operation, so its possible we were cancelled during it.
                    if (CancelRequested)
                    {
                        AcknowledgeCancel();
                        return;
                    }

                    // Link up the initial inline image.
                    ISupportingFile imageFilePlaceholderHolder = this.fileService.CreateSupportingFile(Path.GetFileName(newImage.ImageInfo.ImageSourceUri.LocalPath), Guid.NewGuid().ToString(), inlineImageStream);
                    newImage.ImageData.InlineImageFile = new ImageFileData(imageFilePlaceholderHolder, newImage.InitialSize.Width, newImage.InitialSize.Height, ImageFileRelationship.Inline);
                }
                catch (Exception e)
                {
                    // Something failed for this image, flag this for removal
                    Debug.WriteLine("Image file could not be initialized: " + newImage.ImageInfo.ImageSourceUri.LocalPath + " " + e);
                    Trace.WriteLine("Could not initialize image: " + newImage.ImageInfo.ImageSourceUri.LocalPath);
                    Trace.WriteLine(e.ToString());
                    newImage.Remove = true;
                }
            }

            // We've successfully intialized all the images, but make sure we weren't cancelled at the last second.
            if (CancelRequested)
            {
                AcknowledgeCancel();
                return;
            }
        }