コード例 #1
0
ファイル: ImageTool.cs プロジェクト: femiosinowo/sssadl
        /// <summary>
        /// Resizes an image.  This is the actual image size, not the display size.
        /// </summary>
        /// <param name="resizeWidth">Width in pixels.</param>
        /// <param name="resizeHeight">Height in pixels.</param>
        /// <param name="maintainAspect">Modify parameters to maintain the aspect ratio.</param>
        /// <param name="originalPathFile">Source file, could be the same as the change file.</param>
        /// <param name="changeImagePathFile">The file to receive the change.</param>
        /// <returns>True for success.</returns>
        public bool Resize(int resizeWidth, int resizeHeight, bool maintainAspect, string originalPathFile,
                    string changeImagePathFile)
        {
            bool bRet = false;

            if (null == originalPathFile) return false;
            if (null == changeImagePathFile) return false;

            UseImage(originalPathFile);     // get current image width/height
            if ((m_width == 0) || (m_height == 0)) return false;

            // OK, users will enter in garbage, since they don't know.
            // Let's help them out.
            // (Negative numbers are seen as 0.)
            if ((0 >= resizeWidth) || (0 >= resizeHeight))
            {
                maintainAspect = true;
            }
            if ((0 >= resizeWidth) && (0 >= resizeHeight))
            {
                resizeWidth = 32;  // default
            }

            if (maintainAspect)
            {
                // We need to be sure that our calculations do not overflow.
                double dOx, dOy, dRx, dRy;
                dOx = m_width;
                dOy = m_height;
                dRy = resizeHeight;
                dRx = resizeWidth;

                if (0 >= resizeWidth)
                {
                    //resizeWidth = (resizeHeight * img.Width) / img.Height;
                    //dRx = (dRy * dOx) / dOy;
                    resizeWidth = Convert.ToInt32((dRy * dOx) / dOy);
                }
                else
                {
                    //resizeHeight = (resizeWidth * img.Height) / img.Width;
                    //dRy = (dRx * dOy) / dOx;
                    resizeHeight = Convert.ToInt32((dRx * dOy) / dOx);
                }
            }

            if (originalPathFile.Length > 0)  // may have been set at creation, so not given
                m_originalFile = originalPathFile;

            // Fix slashes - never trust that they have done this.
            m_originalFile = m_originalFile.Replace('/', '\\').Replace("\\\\", "\\");
            changeImagePathFile = changeImagePathFile.Replace('/', '\\').Replace("\\\\", "\\");

            // use cximage library instead of .Net code
            if (m_originalFile != changeImagePathFile)
            {
                // copy file to new if needed
                StorageClient.Context.File.Copy(m_originalFile, changeImagePathFile, true);
                MakeWriteable(changeImagePathFile); // in case the original was readonly
            }
            ImageGalleryInterop.CImageResizerClass ig = new ImageGalleryInterop.CImageResizerClass();
            ig.ResizeImageFile(changeImagePathFile, resizeWidth, resizeHeight);

            /*
            System.Drawing.Image img = System.Drawing.Image.FromFile(m_originalFile);

            System.Drawing.Image.GetThumbnailImageAbort imgCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ImageResizeCallback);

            System.Drawing.Image imgThumbnail = img.GetThumbnailImage(resizeWidth, resizeHeight, imgCallBack, IntPtr.Zero);

            img.Dispose();  // Free up the file
            imgThumbnail.Save(changeImagePathFile);

            imgThumbnail.Dispose();
            */

            // Save for convenient use.
            m_width = resizeWidth;
            m_height = resizeHeight;
            m_localResultFile = changeImagePathFile;

            RetrieveFileSize();

            bRet = true;

            return bRet;
        }