GetBestMatch() 개인적인 메소드

private GetBestMatch ( IList frames, int width, int height ) : System.Windows.Media.Imaging.BitmapFrame
frames IList
width int
height int
리턴 System.Windows.Media.Imaging.BitmapFrame
예제 #1
0
        public static IntPtr GenerateHICON(ImageSource image, Size dimensions)
        {
            if (image == null)
            {
                return(IntPtr.Zero);
            }
            BitmapFrame bitmapFrame = image as BitmapFrame;

            if (bitmapFrame != null)
            {
                bitmapFrame = Utility.GetBestMatch(bitmapFrame.Decoder.Frames, (int)dimensions.Width, (int)dimensions.Height);
            }
            else
            {
                Rect   rectangle = new Rect(0.0, 0.0, dimensions.Width, dimensions.Height);
                double num       = dimensions.Width / dimensions.Height;
                double num2      = image.Width / image.Height;
                if (image.Width <= dimensions.Width && image.Height <= dimensions.Height)
                {
                    rectangle = new Rect((dimensions.Width - image.Width) / 2.0, (dimensions.Height - image.Height) / 2.0, image.Width, image.Height);
                }
                else if (num > num2)
                {
                    double num3 = image.Width / image.Height * dimensions.Width;
                    rectangle = new Rect((dimensions.Width - num3) / 2.0, 0.0, num3, dimensions.Height);
                }
                else if (num < num2)
                {
                    double num4 = image.Height / image.Width * dimensions.Height;
                    rectangle = new Rect(0.0, (dimensions.Height - num4) / 2.0, dimensions.Width, num4);
                }
                DrawingVisual  drawingVisual  = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawImage(image, rectangle);
                drawingContext.Close();
                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)dimensions.Width, (int)dimensions.Height, 96.0, 96.0, PixelFormats.Pbgra32);
                renderTargetBitmap.Render(drawingVisual);
                bitmapFrame = BitmapFrame.Create(renderTargetBitmap);
            }
            IntPtr result;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                new PngBitmapEncoder
                {
                    Frames =
                    {
                        bitmapFrame
                    }
                }.Save(memoryStream);
                using (ManagedIStream managedIStream = new ManagedIStream(memoryStream))
                {
                    IntPtr zero = IntPtr.Zero;
                    try
                    {
                        Status status = NativeMethods.GdipCreateBitmapFromStream(managedIStream, out zero);
                        if (status != Status.Ok)
                        {
                            result = IntPtr.Zero;
                        }
                        else
                        {
                            IntPtr intPtr;
                            status = NativeMethods.GdipCreateHICONFromBitmap(zero, out intPtr);
                            if (status != Status.Ok)
                            {
                                result = IntPtr.Zero;
                            }
                            else
                            {
                                result = intPtr;
                            }
                        }
                    }
                    finally
                    {
                        Utility.SafeDisposeImage(ref zero);
                    }
                }
            }
            return(result);
        }
예제 #2
0
        public static IntPtr GenerateHICON(ImageSource image, Size dimensions)
        {
            IntPtr intPtr;
            IntPtr zero;

            if (image == null)
            {
                return(IntPtr.Zero);
            }
            BitmapFrame bestMatch = image as BitmapFrame;

            if (bestMatch == null)
            {
                Rect   rect  = new Rect(0, 0, dimensions.Width, dimensions.Height);
                double width = dimensions.Width / dimensions.Height;
                double num   = image.Width / image.Height;
                if (image.Width <= dimensions.Width && image.Height <= dimensions.Height)
                {
                    rect = new Rect((dimensions.Width - image.Width) / 2, (dimensions.Height - image.Height) / 2, image.Width, image.Height);
                }
                else if (width > num)
                {
                    double width1 = image.Width / image.Height * dimensions.Width;
                    rect = new Rect((dimensions.Width - width1) / 2, 0, width1, dimensions.Height);
                }
                else if (width < num)
                {
                    double height = image.Height / image.Width * dimensions.Height;
                    rect = new Rect(0, (dimensions.Height - height) / 2, dimensions.Width, height);
                }
                DrawingVisual  drawingVisual  = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawImage(image, rect);
                drawingContext.Close();
                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)dimensions.Width, (int)dimensions.Height, 96, 96, PixelFormats.Pbgra32);
                renderTargetBitmap.Render(drawingVisual);
                bestMatch = BitmapFrame.Create(renderTargetBitmap);
            }
            else
            {
                bestMatch = Utility.GetBestMatch(bestMatch.Decoder.Frames, (int)dimensions.Width, (int)dimensions.Height);
            }
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
                pngBitmapEncoder.Frames.Add(bestMatch);
                pngBitmapEncoder.Save(memoryStream);
                using (ManagedIStream managedIStream = new ManagedIStream(memoryStream))
                {
                    IntPtr zero1 = IntPtr.Zero;
                    try
                    {
                        if (Standard.NativeMethods.GdipCreateBitmapFromStream(managedIStream, out zero1) == Status.Ok)
                        {
                            zero = (Standard.NativeMethods.GdipCreateHICONFromBitmap(zero1, out intPtr) == Status.Ok ? intPtr : IntPtr.Zero);
                        }
                        else
                        {
                            zero = IntPtr.Zero;
                        }
                    }
                    finally
                    {
                        Utility.SafeDisposeImage(ref zero1);
                    }
                }
            }
            return(zero);
        }