コード例 #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);
            }
        }
コード例 #2
0
 private BlogPostImageData(ImageFileData imageSource, ImageFileData imageShadowSource, ImageFileData inlineImageFile, ImageFileData linkedImageFile, BlogPostImageServiceUploadInfo uploadInfo, BlogPostSettingsBag decoratorSettings)
 {
     ImageSourceFile        = imageSource;
     ImageSourceShadowFile  = imageShadowSource;
     InlineImageFile        = inlineImageFile;
     LinkedImageFile        = linkedImageFile;
     UploadInfo             = uploadInfo;
     ImageDecoratorSettings = decoratorSettings;
 }
コード例 #3
0
 private BlogPostImageData(ImageFileData imageSource, ImageFileData imageShadowSource, ImageFileData inlineImageFile, ImageFileData linkedImageFile, BlogPostImageServiceUploadInfo uploadInfo, BlogPostSettingsBag decoratorSettings)
 {
     ImageSourceFile = imageSource;
     ImageSourceShadowFile = imageShadowSource;
     InlineImageFile = inlineImageFile;
     LinkedImageFile = linkedImageFile;
     UploadInfo = uploadInfo;
     ImageDecoratorSettings = decoratorSettings;
 }
コード例 #4
0
        public static BlogPostImageData LookupImageDataByInlineUri(BlogPostImageDataList imageDataList, Uri inlineUri)
        {
            foreach (BlogPostImageData imageData in imageDataList)
            {
                ImageFileData fileData = imageData.InlineImageFile;

                //Check for condition that caused bug 483278, but we couldn't repro, investigate how we get into this state.
                Debug.Assert(fileData != null && fileData.Uri != null, "Illegal state for filedata detected!");

                if (fileData != null && fileData.Uri != null && fileData.Uri.Equals(inlineUri))
                {
                    return(imageData);
                }
            }
            return(null);
        }
コード例 #5
0
            public void WriteImageFiles(XmlTextWriter writer, object imageFiles)
            {
                writer.WriteStartElement(IMAGE_FILES_ELEMENT);
                foreach (BlogPostImageData imageInfo in (imageFiles as BlogPostImageDataList))
                {
                    if (IsReferencedImage(imageInfo))
                    {
                        ImageFileData[] imageFileArray = new ImageFileData[] { imageInfo.ImageSourceFile, imageInfo.InlineImageFile, imageInfo.LinkedImageFile, imageInfo.ImageSourceShadowFile };
                        writer.WriteStartElement(IMAGE_FILE_ELEMENT);

                        for (int i = 0; i < imageFileArray.Length; i++)
                        {
                            ImageFileData imageFile = imageFileArray[i];
                            if (imageFile != null)
                            {
                                writer.WriteStartElement(SUPPORTING_FILE_LINK_ELEMENT);
                                writer.WriteAttributeString(SUPPORTING_FILE_LINK_FILE_ID_ATTRIBUTE, imageFile.SupportingFile.FileId);
                                writer.WriteEndElement(); //end SUPPORTING_FILE_LINK_ELEMENT
                            }
                        }

                        //write out the image decorator settings
                        WriteBlogPostSettingsBag(writer, imageInfo.ImageDecoratorSettings, IMAGE_DECORATORS_SETTINGSBAG_NAME);

                        writer.WriteStartElement(IMAGE_UPLOAD_ELEMENT);
                        WriteNonNullAttribute(writer, IMAGE_UPLOAD_SERVICE_ID_ATTRIBUTE, imageInfo.UploadInfo.ImageServiceId);
                        WriteBlogPostSettingsBag(writer, imageInfo.UploadInfo.Settings, IMAGE_UPLOAD_SETTINGSBAG_NAME);
                        writer.WriteEndElement(); //end IMAGE_UPLOAD_ELEMENT

                        writer.WriteEndElement(); //end IMAGE_FILE_ELEMENT
                    }
                }
                writer.WriteEndElement();	//end IMAGE_FILES_ELEMENT
            }
コード例 #6
0
 private bool IsReferencedImage(BlogPostImageData imageInfo)
 {
     ImageFileData[] imageFileArray = new ImageFileData[] { imageInfo.InlineImageFile, imageInfo.LinkedImageFile };
     foreach (ImageFileData imageFile in imageFileArray)
     {
         if (imageFile != null && _referenceList.IsReferenced(imageFile.SupportingFile))
         {
             return true;
         }
     }
     return false;
 }
コード例 #7
0
            public object ReadImageFiles(XmlTextReader reader)
            {
                BlogPostImageDataList imageFiles = new BlogPostImageDataList();
                BlogPostImageData blogPostImageData = null;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName == IMAGE_FILE_ELEMENT)
                        {
                            blogPostImageData = new BlogPostImageData();
                        }
                        else if (reader.LocalName == IMAGE_FILE_LINK_ELEMENT || reader.LocalName == SUPPORTING_FILE_LINK_ELEMENT)
                        {
                            if (blogPostImageData == null)
                                throw PostEditorStorageException.Create(new StorageInvalidFormatException());

                            ImageFileData imageFileData;
                            if (reader.LocalName == IMAGE_FILE_LINK_ELEMENT) //BACKWARDS_COMPATABILITY: used for backward compatibility with pre-Beta2 Writer files
                            {
                                ISupportingFile supportingFile;
                                string storagePath = reader.GetAttribute(IMAGE_FILE_LINK_PATH_ATTRIBUTE);

                                //the old image data saves a "corrupted" lowercase version of the embedded URL,
                                //so we need to take that into account and fix it
                                bool embeddedFile =
                                    storagePath.StartsWith(SupportingFilePersister.SUPPORTING_FILE_PREFIX.ToLower(CultureInfo.InvariantCulture));
                                if (embeddedFile)
                                {
                                    //fix up the corrupted storage path URL so that the reference fixer can resolve it.
                                    storagePath = storagePath.Replace(SupportingFilePersister.SUPPORTING_FILE_PREFIX.ToLower(CultureInfo.InvariantCulture),
                                        SupportingFilePersister.SUPPORTING_FILE_PREFIX).TrimEnd('/');

                                    //convert the internal storage path to the local temp path
                                    storagePath = _supportingFilePersister.LoadFilesAndFixupReferences(new string[] { storagePath })[0];

                                    //register the supporting file
                                    supportingFile = _fileService.CreateSupportingFileFromStoragePath(Path.GetFileName(storagePath), storagePath);
                                }
                                else
                                {
                                    //register the linked supporting file
                                    supportingFile = _fileService.AddLinkedSupportingFileReference(new Uri(storagePath));
                                }

                                imageFileData = new ImageFileData(supportingFile);
                                for (int i = 0; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToAttribute(i);
                                    switch (reader.LocalName)
                                    {
                                        case IMAGE_FILE_LINK_WIDTH_ATTRIBUTE:
                                            imageFileData.Width = Int32.Parse(reader.Value, CultureInfo.InvariantCulture);
                                            break;
                                        case IMAGE_FILE_LINK_HEIGHT_ATTRIBUTE:
                                            imageFileData.Height = Int32.Parse(reader.Value, CultureInfo.InvariantCulture);
                                            break;
                                        case IMAGE_FILE_LINK_RELATIONSHIP_ATTRIBUTE:
                                            imageFileData.Relationship = (ImageFileRelationship)ImageFileRelationship.Parse(typeof(ImageFileRelationship), reader.Value);
                                            break;
                                        case IMAGE_FILE_LINK_PUBLISH_URL_ATTRIBUTE: //BACKWARDS_COMPATABILITY: not compatible with Beta2
                                            //imageFileData.PublishedUri = new Uri(reader.Value);
                                            break;
                                    }
                                }
                            }
                            else
                            {
                                string fileId = reader.GetAttribute(SUPPORTING_FILE_LINK_FILE_ID_ATTRIBUTE);
                                ISupportingFile supportingFile = _fileService.GetFileById(fileId);
                                if (supportingFile != null)
                                {
                                    imageFileData = new ImageFileData(supportingFile);
                                }
                                else
                                {
                                    Debug.Fail("Invalid post file state detected: image is referencing a file that is not attached");
                                    imageFileData = null;
                                }
                            }
                            if (imageFileData != null)
                            {
                                if (imageFileData.Relationship == ImageFileRelationship.Inline)
                                    blogPostImageData.InlineImageFile = imageFileData;
                                else if (imageFileData.Relationship == ImageFileRelationship.Linked)
                                    blogPostImageData.LinkedImageFile = imageFileData;
                                else if (imageFileData.Relationship == ImageFileRelationship.Source)
                                    blogPostImageData.ImageSourceFile = imageFileData;
                                else if (imageFileData.Relationship == ImageFileRelationship.SourceShadow)
                                    blogPostImageData.ImageSourceShadowFile = imageFileData;
                                else
                                    Debug.Fail("Unknown image file relationship detected: " + imageFileData.Relationship.ToString());
                            }
                        }
                        else if (reader.LocalName == IMAGE_UPLOAD_ELEMENT)
                        {
                            for (int i = 0; i < reader.AttributeCount; i++)
                            {
                                reader.MoveToAttribute(i);
                                switch (reader.LocalName)
                                {
                                    case IMAGE_UPLOAD_SERVICE_ID_ATTRIBUTE:
                                        blogPostImageData.UploadInfo.ImageServiceId = reader.Value;
                                        break;
                                }
                            }
                        }
                        else if (reader.LocalName == SETTINGS_BAG_ELEMENT)
                        {
                            BlogPostSettingsBag settings = new BlogPostSettingsBag();
                            string settingsBagName = ReadBlogPostSettingsBag(reader, settings);
                            if (settingsBagName == IMAGE_DECORATORS_SETTINGSBAG_NAME)
                            {
                                blogPostImageData.ImageDecoratorSettings = settings;
                            }
                            else if (settingsBagName == IMAGE_UPLOAD_SETTINGSBAG_NAME)
                            {
                                blogPostImageData.UploadInfo.Settings = settings;
                            }
                            else
                                Debug.Fail("Unknown settings bag encountered: " + settingsBagName);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == IMAGE_FILE_ELEMENT)
                    {
                        //initialize the shadow file if one doesn't already exist. (necessary for pre-beta2 post files)
                        blogPostImageData.InitShadowFile(_fileService);

                        imageFiles.AddImage(blogPostImageData);
                        blogPostImageData = null;
                    }
                }
                return imageFiles;
            }
コード例 #8
0
 public BlogPostImageData(ImageFileData imageSource) : this(imageSource, null, null, null, new BlogPostImageServiceUploadInfo(), new BlogPostSettingsBag())
 {
 }
コード例 #9
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);
            }
        }
コード例 #10
0
 public BlogPostImageData(ImageFileData imageSource) : this(imageSource, null, null, null, new BlogPostImageServiceUploadInfo(), new BlogPostSettingsBag())
 {
 }