示例#1
0
        private void btnOpen_Click(Object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog()
            {
                Filter      = "PNG,JPG,BMP|*.png;*.jpg;*.bmp",
                Title       = "Select An Image File",
                Multiselect = false
            };
            var result = dlg.ShowDialog();

            if (result == false)
            {
                return;
            }
            try
            {
                imageInf = new ImageInf(dlg.FileName);
            }
            catch (Exception ex)
            {
                if (ex.Message == "Not Image Exception")
                {
                    MessageBox.Show("File is not an image");
                    return;
                }
            }
            imgPreview.Source = new BitmapImage(new Uri(imageInf.FilePath));
            txbType.Text      = imageInf.Type.ToString();
            txbWidth.Text     = imageInf.Width.ToString();
            txbHeight.Text    = imageInf.Height.ToString();
            txbTip.Visibility = Visibility.Hidden;

            if (imageInf.Width >= imageInf.Height && imageInf.Width > 512)
            {
                iudWidth.Value  = 512;
                iudHeight.Value = 512 * imageInf.Height / imageInf.Width;
            }
            else if (imageInf.Width < imageInf.Height && imageInf.Height > 512)
            {
                iudHeight.Value = 512;
                iudWidth.Value  = 512 * imageInf.Width / imageInf.Height;
            }
            else if (imageInf.Height <= 512 && imageInf.Width <= 512)
            {
                iudHeight.Value = imageInf.Height;
                iudWidth.Value  = imageInf.Width;
            }
        }
示例#2
0
        /// <summary>
        /// Convert Image To Icon
        /// </summary>
        /// <param name="image">Image</param>
        /// <returns>Icon</returns>
        // See Also http://blog.csdn.net/wangzibigan/article/details/79121924
        public static Icon ConvertToIcon(ImageInf imageinf)
        {
            using (var msImg = new MemoryStream())
            {
                using (var msIco = new MemoryStream())
                {
                    // Save Image As PNG And Put Into Stream
                    using (Bitmap bmp = new Bitmap(imageinf.Image, new Size(imageinf.TargetWidth, imageinf.TargetHeight)))
                    {
                        bmp.Save(msImg, ImageFormat.Png);
                    }
                    using (var bin = new BinaryWriter(msIco))
                    {
                        IconInf iconInf = new IconInf()
                        {
                            Width     = (Byte)imageinf.TargetWidth,
                            Height    = (Byte)imageinf.TargetHeight,
                            ImageSize = (Int32)msImg.Length,
                            ImageData = msImg.ToArray()
                        };

                        // Write Icon
                        bin.Write(iconInf.Header);
                        bin.Write(iconInf.Width);
                        bin.Write(iconInf.Height);
                        bin.Write(iconInf.ColorNum);
                        bin.Write(iconInf.Reserved);
                        bin.Write(iconInf.Planes);
                        bin.Write(iconInf.PixelBit);
                        bin.Write(iconInf.ImageSize);
                        bin.Write(iconInf.ImageOffSet);
                        bin.Write(iconInf.ImageData);
                        bin.Flush();
                        bin.Seek(0, SeekOrigin.Begin);

                        iconInf.Dispose();
                        return(new Icon(msIco));
                    }
                }
            }
        }