Wpf, 圖片縮小程式

摘要:Wpf, 圖片縮小程式

  /// <summary>
        /// 圖片縮小
        /// </summary>
        /// <param name="filepath">檔案路徑</param>
        /// <returns>縮小後的圖片位置</returns>
        public string ImgResize(string filepath)
        {
            string result = null;
            if (!String.IsNullOrEmpty(filepath))
            {                         
                try
                {
                    System.Windows.Controls.Image img = new System.Windows.Controls.Image();                 
                    Bitmap bmp = new Bitmap(filepath);
                  
                    int filewidth = 0;
                    int fileheight = 0;
                    if (bmp.Width == bmp.Height)
                    {
                        filewidth = 150;
                        fileheight = 150;
                    }
                    else
                    {
                        if (bmp.Height > bmp.Width)
                        {
                            filewidth = 150;
                            fileheight = (bmp.Height * 150) / bmp.Width;
                        }
                        else
                        {
                            fileheight = 150;
                            filewidth = (bmp.Width * 150) / bmp.Height;
                        }
                    }
                  
                    int tfindex = filepath.LastIndexOf('\\');
                    int afindex = filepath.LastIndexOf('.');

                    string tempfilepath = filepath.Substring(0, tfindex);
                    string getfilename = filepath.Substring(tfindex + 1);
                    Console.WriteLine("getfilename:" + getfilename);
                    getfilename = getfilename.Substring(0, getfilename.LastIndexOf('.'));
                    string getsubfilename = filepath.Substring(afindex + 1);
                    string tempDir = fun.GetReceiveFilePath(dm.RECEIVEFILEDIR) + dm.MYPICDIR;
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }
                    string tempfilename = tempDir + "\\" + getfilename + ".png";
                    Console.WriteLine("tempfilepath:" + tempfilepath);
                    Console.WriteLine("getfilename:" + getfilename);
                    Console.WriteLine("tempfilename:" + tempfilename);

                 
                    //Retrieve the image filename whose thumbnail has to be created

 

                    //the uploaded image will be stored in the Pics folder.
                    //to get resize the image, the original image has to be
                    //accessed from the Pics folder                  

                    log.Debug("Start ImgResize");
                    System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(filepath);
                    log.Debug("set GetThumbnailImageAbort");
                    System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                    log.Debug("GetThumbnailImage");
                    System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(filewidth, fileheight, dummyCallBack, IntPtr.Zero);

                    //We need to create a unique filename for each generated image                 
                    //Save the thumbnail in PNG format.
                    //You may change it to a diff format with the ImageFormat property
                    log.Debug("Save");
                    thumbNailImg.Save(tempfilename, System.Drawing.Imaging.ImageFormat.Png);
                    log.Debug("after Save");
                    _MYPICMD5 = MD5Lib.CalcFileMD5(tempfilename);
                    result = tempfilename;
                    thumbNailImg.Dispose();
                    bmp.Dispose();
                }
                catch (Exception ex)
                {
                    log.Error("ImgResize ex:" + ex.Message);
                    Console.WriteLine("ex:" + ex.Message);
                }            

            }
            return result;
        }

 

public bool ThumbnailCallback()
        {
            log.Error("ImgResize ThumbnailCallback");
            return false;
        }