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; } }
void ISupportingFiles.AddImage(string fileName, Image image, ImageFormat imageFormat) { // convert the bitmap into a stream using (MemoryStream imageStream = new MemoryStream()) { // save the image into the strea if (image is Bitmap) { ImageHelper2.SaveImage((Bitmap)image, imageFormat, imageStream); } else { image.Save(imageStream, imageFormat); } // add the stream to the supporting file imageStream.Seek(0, SeekOrigin.Begin); _extensionData.AddFile(fileName, imageStream); } }
/// <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; } }