Ejemplo n.º 1
0
        private string BuildThumb(PhysicalFileFromTemp file)
        {
            return(string.Empty);

            string thumbPathRoot = IOUtil.ResolvePath(ThumbRoot);
            string extendName    = Path.GetExtension(file.TempUploadFileName);

            string level1 = file.MD5.Substring(0, 1);
            string level2 = file.MD5.Substring(1, 1);
            string level3 = file.MD5.Substring(2, 1);

            extendName = extendName.ToLower();

            string thumbFilename = string.Format("{0}_{1}.png", file.MD5, file.FileSize);
            string dir           = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3);

            try
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }
            catch
            {
                return(string.Empty);
            }

            string thumbFilePath = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3, thumbFilename);
            string thumbUrl      = UrlUtil.JoinUrl(ThumbRoot, level1, level2, level3, thumbFilename);

            switch (extendName)
            {
            case ".jpg":
            case ".jpge":
            case ".bmp":
            case ".png":
            case ".gif":

                Image img = null, imgThumb = null;
                try
                {
                    img = Bitmap.FromFile(file.PhysicalFilePath);
                }
                catch    // (Exception ex)
                {
                    return(string.Empty);
                }

                using (imgThumb = new Bitmap(ThumbSize, ThumbSize))
                {
                    int   x, y, w, h;
                    float scale = (float)img.Width / (float)img.Height;
                    if (img.Width > img.Height)
                    {
                        x = 0; w = ThumbSize;
                        h = (int)((float)w / scale);
                        y = (ThumbSize - h) / 2;
                    }
                    else if (img.Width == img.Height)
                    {
                        x = 0;
                        y = 0;
                        w = ThumbSize;
                        h = ThumbSize;
                    }
                    else
                    {
                        y = 0;
                        h = ThumbSize;
                        w = (int)((float)ThumbSize * scale);
                        x = (ThumbSize - w) / 2;
                    }

                    using (Graphics g = Graphics.FromImage(imgThumb))
                    {
                        g.Clear(Color.White);
                        g.DrawImage(img, new Rectangle(x, y, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
                        g.Save();
                    }
                    try
                    {
                        img.Save(thumbFilePath, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    catch
                    {
                        return(string.Empty);
                    }
                }
                img.Dispose();
                return(thumbUrl);

            default:
                break;
            }

            return(string.Empty);
        }