示例#1
0
        protected void RenderCompositionTo(Surface dst, bool highQuality, bool forceUpToDate)
        {
            if (forceUpToDate)
            {
                UpdateComposition(false);
            }

            if (dst.Width == this.compositionSurface.Width && 
                dst.Height == this.compositionSurface.Height)
            {
                dst.ClearWithCheckboardPattern();
                new UserBlendOps.NormalBlendOp().Apply(dst, this.compositionSurface);
            }
            else if (highQuality)
            {
                Surface thumb = new Surface(dst.Size);
                thumb.SuperSamplingFitSurface(this.compositionSurface);

                dst.ClearWithCheckboardPattern();

                new UserBlendOps.NormalBlendOp().Apply(dst, thumb);

                thumb.Dispose();
            }
            else
            {
                this.surfaceBox.RenderTo(dst);
            }
        }
示例#2
0
        public override Surface RenderThumbnail(int maxEdgeLength)
        {
            Size thumbSize = Utility.ComputeThumbnailSize(this.Size, maxEdgeLength);
            Surface thumb = new Surface(thumbSize);

            thumb.SuperSamplingFitSurface(this.surface);

            Surface thumb2 = new Surface(thumbSize);
            thumb2.ClearWithCheckboardPattern();
            UserBlendOps.NormalBlendOp nbop = new UserBlendOps.NormalBlendOp();
            nbop.Apply(thumb2, thumb);

            thumb.Dispose();
            thumb = null;

            return thumb2;
        }
示例#3
0
        public override Surface RenderThumbnail(int maxEdgeLength)
        {
            Size    thumbSize = Utility.ComputeThumbnailSize(this.Size, maxEdgeLength);
            Surface thumb     = new Surface(thumbSize);

            thumb.SuperSamplingFitSurface(this.surface);

            Surface thumb2 = new Surface(thumbSize);

            thumb2.ClearWithCheckboardPattern();
            UserBlendOps.NormalBlendOp nbop = new UserBlendOps.NormalBlendOp();
            nbop.Apply(thumb2, thumb);

            thumb.Dispose();
            thumb = null;

            return(thumb2);
        }
示例#4
0
        /// <summary>
        /// Does the dirty work for a File->Save operation. If any of the "Save Options" in the
        /// DocumentWorkspace are null, this will call DoSaveAs(). If the image has more than 1
        /// layer but the file type they want to save with does not support layers, then it will
        /// ask the user about flattening the image.
        /// </summary>
        /// <param name="tryToFlatten">
        /// If true, will ask the user about flattening if the workspace's saveFileType does not 
        /// support layers and the image has more than 1 layer.
        /// If false, then DoSaveAs will be called and the fileType will be prepopulated with
        /// the .PDN type.
        /// </param>
        /// <returns><b>true</b> if the file was saved, <b>false</b> if the user cancelled</returns>
        protected bool DoSave(bool tryToFlatten)
        {
            using (new PushNullToolMode(this))
            {
                string newFileName;
                FileType newFileType;
                SaveConfigToken newSaveConfigToken;

                GetDocumentSaveOptions(out newFileName, out newFileType, out newSaveConfigToken);

                // if they haven't specified a filename, then revert to "Save As" behavior
                if (newFileName == null)
                {
                    return DoSaveAs();
                }

                // if we have a filename but no file type, try to infer the file type
                if (newFileType == null)
                {
                    FileTypeCollection fileTypes = FileTypes.GetFileTypes();
                    string ext = Path.GetExtension(newFileName);
                    int index = fileTypes.IndexOfExtension(ext);
                    FileType inferredFileType = fileTypes[index];
                    newFileType = inferredFileType;
                }

                // if the image has more than 1 layer but is saving with a file type that
                // does not support layers, then we must ask them if we may flatten the
                // image first
                if (Document.Layers.Count > 1 && !newFileType.SupportsLayers)
                {
                    if (!tryToFlatten)
                    {
                        return DoSaveAs();
                    }
                    else
                    {
                        DialogResult dr = WarnAboutFlattening();

                        if (dr == DialogResult.Yes)
                        {
                            ExecuteFunction(new FlattenFunction());
                        }
                        else
                        {
                            return false;
                        }
                    }
                }

                // get the configuration!
                if (newSaveConfigToken == null)
                {
                    Surface scratch = BorrowScratchSurface(this.GetType().Name + ".DoSave() calling GetSaveConfigToken()");

                    bool result;
                    try
                    {
                        result = GetSaveConfigToken(newFileType, newSaveConfigToken, out newSaveConfigToken, scratch);
                    }

                    finally
                    {
                        ReturnScratchSurface(scratch);
                    }

                    if (!result)
                    {
                        return false;
                    }
                }

                // At this point fileName, fileType, and saveConfigToken must all be non-null

                // if the document supports custom headers, embed a thumbnail in there
                if (newFileType.SupportsCustomHeaders)
                {
                    using (new WaitCursorChanger(this))
                    {
                        Utility.GCFullCollect();
                        const int maxDim = 256;

                        Surface thumb;
                        Surface flattened = BorrowScratchSurface(this.GetType().Name + ".DoSave() preparing embedded thumbnail");

                        try
                        {
                            Document.Flatten(flattened);

                            if (Document.Width > maxDim || Document.Height > maxDim)
                            {
                                int width;
                                int height;

                                if (Document.Width > Document.Height)
                                {
                                    width = maxDim;
                                    height = (Document.Height * maxDim) / Document.Width;
                                }
                                else
                                {
                                    height = maxDim;
                                    width = (Document.Width * maxDim) / Document.Height;
                                }

                                int thumbWidth = Math.Max(1, width);
                                int thumbHeight = Math.Max(1, height);

                                thumb = new Surface(thumbWidth, thumbHeight);
                                thumb.SuperSamplingFitSurface(flattened);
                            }
                            else
                            {
                                thumb = new Surface(flattened.Size);
                                thumb.CopySurface(flattened);
                            }
                        }

                        finally
                        {
                            ReturnScratchSurface(flattened);
                        }

                        Document thumbDoc = new Document(thumb.Width, thumb.Height);
                        BitmapLayer thumbLayer = new BitmapLayer(thumb);
                        BitmapLayer backLayer = new BitmapLayer(thumb.Width, thumb.Height);
                        backLayer.Surface.Clear(ColorBgra.Transparent);
                        thumb.Dispose();
                        thumbDoc.Layers.Add(backLayer);
                        thumbDoc.Layers.Add(thumbLayer);
                        MemoryStream thumbPng = new MemoryStream();
                        PropertyBasedSaveConfigToken pngToken = PdnFileTypes.Png.CreateDefaultSaveConfigToken();
                        PdnFileTypes.Png.Save(thumbDoc, thumbPng, pngToken, null, null, false);
                        byte[] thumbBytes = thumbPng.ToArray();

                        string thumbString = Convert.ToBase64String(thumbBytes, Base64FormattingOptions.None);
                        thumbDoc.Dispose();

                        string thumbXml = "<thumb png=\"" + thumbString + "\" />";
                        Document.CustomHeaders = thumbXml;
                    }
                }

                // save!
                bool success = false;
                Stream stream = null;

                try
                {
                    stream = (Stream)new FileStream(newFileName, FileMode.Create, FileAccess.Write);

                    using (new WaitCursorChanger(this))
                    {
                        Utility.GCFullCollect();

                        SaveProgressDialog sd = new SaveProgressDialog(this);
                        Surface scratch = BorrowScratchSurface(this.GetType().Name + ".DoSave() handing off scratch surface to SaveProgressDialog.Save()");

                        try
                        {
                            sd.Save(stream, Document, newFileType, newSaveConfigToken, scratch);
                        }

                        finally
                        {
                            ReturnScratchSurface(scratch);
                        }

                        success = true;

                        this.lastSaveTime = DateTime.Now;

                        stream.Close();
                        stream = null;
                    }
                }

                catch (UnauthorizedAccessException)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.UnauthorizedAccessException"));
                }

                catch (SecurityException)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.SecurityException"));
                }

                catch (DirectoryNotFoundException)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.DirectoryNotFoundException"));
                }

                catch (IOException)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.IOException"));
                }

                catch (OutOfMemoryException)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.OutOfMemoryException"));
                }

#if !DEBUG
                catch (Exception)
                {
                    Utility.ErrorBox(this, PdnResources.GetString("SaveImage.Error.Exception"));
                }
#endif

                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                }

                if (success)
                {
                    Shell.AddToRecentDocumentsList(newFileName);
                }
                else
                {
                    return false;
                }

                // reset the dirty bit so they won't be asked to save on quitting
                Document.Dirty = false;

                // some misc. book keeping ...
                AddToMruList();

                // and finally, shout happiness by way of ...
                return true;
            }
        }