Beispiel #1
0
        // This makes a preview for the given image and updates the image settings
        private void MakeImagePreview(ImageData img)
        {
            lock (img)
            {
                // Load image if needed
                if (!img.IsImageLoaded)
                {
                    img.LoadImage();
                }
                int    imagewidth, imageheight;
                Bitmap image = img.GetBitmap();                 //mxd
                Bitmap preview;
                lock (image)
                {
                    if (!img.LoadFailed)
                    {
                        imagewidth  = img.Width;
                        imageheight = img.Height;
                    }
                    else
                    {
                        Size size = image.Size; //mxd
                        imagewidth  = size.Width;
                        imageheight = size.Height;
                    }

                    // Determine preview size
                    float scalex        = (img.Width > MAX_PREVIEW_SIZE) ? (MAX_PREVIEW_SIZE / (float)imagewidth) : 1.0f;
                    float scaley        = (img.Height > MAX_PREVIEW_SIZE) ? (MAX_PREVIEW_SIZE / (float)imageheight) : 1.0f;
                    float scale         = Math.Min(scalex, scaley);
                    int   previewwidth  = (int)(imagewidth * scale);
                    int   previewheight = (int)(imageheight * scale);
                    if (previewwidth < 1)
                    {
                        previewwidth = 1;
                    }
                    if (previewheight < 1)
                    {
                        previewheight = 1;
                    }

                    //mxd. Expected and actual image sizes and format match?
                    if (previewwidth == imagewidth && previewheight == imageheight && image.PixelFormat == IMAGE_FORMAT)
                    {
                        preview = new Bitmap(image);
                    }
                    else
                    {
                        // Make new image
                        preview = new Bitmap(previewwidth, previewheight, IMAGE_FORMAT);
                        Graphics g = Graphics.FromImage(preview);
                        g.PageUnit = GraphicsUnit.Pixel;
                        //g.CompositingQuality = CompositingQuality.HighQuality; //mxd
                        g.InterpolationMode = InterpolationMode.NearestNeighbor;
                        //g.SmoothingMode = SmoothingMode.HighQuality; //mxd
                        g.PixelOffsetMode = PixelOffsetMode.None;
                        //g.Clear(Color.Transparent); //mxd

                        // Draw image onto atlas
                        Rectangle  atlasrect = new Rectangle(0, 0, previewwidth, previewheight);
                        RectangleF imgrect   = General.MakeZoomedRect(new Size(imagewidth, imageheight), atlasrect);
                        if (imgrect.Width < 1.0f)
                        {
                            imgrect.X    -= 0.5f - imgrect.Width * 0.5f;
                            imgrect.Width = 1.0f;
                        }
                        if (imgrect.Height < 1.0f)
                        {
                            imgrect.Y     -= 0.5f - imgrect.Height * 0.5f;
                            imgrect.Height = 1.0f;
                        }
                        g.DrawImage(image, imgrect);
                        g.Dispose();
                    }
                }

                // Unload image if no longer needed
                if (!img.IsReferenced)
                {
                    img.UnloadImage();
                }

                lock (images)
                {
                    // Set numbers
                    img.PreviewIndex = images.Count;
                    img.PreviewState = ImageLoadState.Ready;

                    // Add to previews list
                    images.Add(preview);
                }
            }
        }
Beispiel #2
0
        // This makes a preview for the given image and updates the image settings
        private void MakeImagePreview(ImageData img)
        {
            int      previewwidth, previewheight;
            int      imagewidth, imageheight;
            Bitmap   preview;
            Graphics g;

            lock (img)
            {
                // Load image if needed
                if (!img.IsImageLoaded)
                {
                    img.LoadImage();
                }
                if (!img.LoadFailed)
                {
                    imagewidth  = img.Width;
                    imageheight = img.Height;
                }
                else
                {
                    imagewidth  = img.GetBitmap().Size.Width;
                    imageheight = img.GetBitmap().Size.Height;
                }

                // Determine preview size
                float scalex = (img.Width > maxpreviewwidth) ? ((float)maxpreviewwidth / (float)imagewidth) : 1.0f;
                float scaley = (img.Height > maxpreviewheight) ? ((float)maxpreviewheight / (float)imageheight) : 1.0f;
                float scale  = Math.Min(scalex, scaley);
                previewwidth  = (int)((float)imagewidth * scale);
                previewheight = (int)((float)imageheight * scale);
                if (previewwidth < 1)
                {
                    previewwidth = 1;
                }
                if (previewheight < 1)
                {
                    previewheight = 1;
                }

                // Make new image
                preview              = new Bitmap(previewwidth, previewheight, IMAGE_FORMAT);
                g                    = Graphics.FromImage(preview);
                g.PageUnit           = GraphicsUnit.Pixel;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode      = SmoothingMode.HighQuality;
                g.PixelOffsetMode    = PixelOffsetMode.None;
                g.Clear(Color.Transparent);

                // Draw image onto atlas
                Rectangle  atlasrect = new Rectangle(0, 0, previewwidth, previewheight);
                RectangleF imgrect   = General.MakeZoomedRect(new Size(imagewidth, imageheight), atlasrect);
                if (imgrect.Width < 1.0f)
                {
                    imgrect.X    -= 0.5f - imgrect.Width * 0.5f;
                    imgrect.Width = 1.0f;
                }
                if (imgrect.Height < 1.0f)
                {
                    imgrect.Y     -= 0.5f - imgrect.Height * 0.5f;
                    imgrect.Height = 1.0f;
                }
                g.DrawImage(img.GetBitmap(), imgrect);
                g.Dispose();

                // Unload image if no longer needed
                if (!img.IsReferenced)
                {
                    img.UnloadImage();
                }

                lock (images)
                {
                    // Set numbers
                    img.PreviewIndex = images.Count;
                    img.PreviewState = ImageLoadState.Ready;

                    // Add to previews list
                    images.Add(preview);
                }
            }
        }