コード例 #1
0
        public static bool extract_thumbnail(Guid?applicationId, DocFileInfo sourceFile, byte[] sourceContent,
                                             DocFileInfo destFile, int x, int y, int width, int height, int thumbnailWidth, int thumbnailHeight,
                                             ref Image retImage, ref string message, ref IconMeta meta)
        {
            try
            {
                if (sourceContent == null || sourceContent.Length == 0)
                {
                    return(false);
                }

                bool result = true;

                using (MemoryStream stream = new MemoryStream(sourceContent))
                    using (Bitmap image = Bitmap.FromStream(stream) as Bitmap)
                    {
                        if (image.Width < thumbnailWidth || image.Height < thumbnailHeight)
                        {
                            message = "{\"ErrorText\":\"" + Messages.ImageSizeIsNotValid.ToString() + "\"}";
                            image.Dispose();
                            return(false);
                        }

                        if (x < 0 || y < 0 || width <= 0 || height <= 0)
                        {
                            double aspectRatio = (double)thumbnailWidth / (double)thumbnailHeight;
                            int    min         = ((double)image.Width / (double)thumbnailWidth) < ((double)image.Height / (double)thumbnailHeight) ?
                                                 image.Width : image.Height;

                            if (min == image.Width)
                            {
                                width  = image.Width;
                                height = (int)Math.Floor((double)width / aspectRatio);
                            }
                            else
                            {
                                height = image.Height;
                                width  = (int)Math.Floor((double)height * aspectRatio);
                            }

                            x = (image.Width - width) / 2;
                            y = (image.Height - height) / 2;
                        }

                        meta = new IconMeta()
                        {
                            X               = x,
                            Y               = y,
                            Width           = width,
                            Height          = height,
                            Icon            = destFile,
                            HighQualityIcon = sourceFile
                        };

                        message = meta.toJson(applicationId);

                        Rectangle rect = new Rectangle(x, y, width, height);

                        using (Bitmap target = new Bitmap(rect.Width, rect.Height))
                        {
                            Graphics g = Graphics.FromImage(target);
                            g.DrawImage(image, new Rectangle(0, 0, target.Width, target.Height), rect, GraphicsUnit.Pixel);

                            result = make_thumbnail(target as Image,
                                                    thumbnailWidth, thumbnailHeight, 0, 0, ref retImage, ref message, true);
                        }

                        if (!result)
                        {
                            message = "{\"ErrorText\":\"" + Messages.OperationFailed + "\"}";
                        }
                        return(result);
                    }
            }
            catch { message = "{\"ErrorText\":\"" + Messages.OperationFailed + "\"}"; return(false); }
        }