Ejemplo n.º 1
0
        public static ImageInterpolation Convert(SD.Drawing2D.InterpolationMode value)
        {
            switch (value)
            {
            case SD.Drawing2D.InterpolationMode.NearestNeighbor:
                return(ImageInterpolation.None);

            case SD.Drawing2D.InterpolationMode.Low:
                return(ImageInterpolation.Low);

            case SD.Drawing2D.InterpolationMode.High:
                return(ImageInterpolation.Medium);

            case SD.Drawing2D.InterpolationMode.HighQualityBilinear:
                return(ImageInterpolation.High);

            case SD.Drawing2D.InterpolationMode.Default:
                return(ImageInterpolation.Default);

            case SD.Drawing2D.InterpolationMode.HighQualityBicubic:
            case SD.Drawing2D.InterpolationMode.Bicubic:
            case SD.Drawing2D.InterpolationMode.Bilinear:
            default:
                throw new NotSupportedException();
            }
        }
Ejemplo n.º 2
0
        public void GenerateImage()
        {
            if (_map == null)
            {
                _image = null;
                return;
            }

            idleX = 0;
            idleY = 0;

            Func <bool> OnIdleGenerateImage = () => {
                int x = idleX;
                int y = idleY;

                if (idleY >= _map.MapHeight)
                {
                    return(false);
                }

                int roomWidth  = (int)(_map.RoomWidth * 16 * scale);
                int roomHeight = (int)(_map.RoomHeight * 16 * scale);

                const System.Drawing.Drawing2D.InterpolationMode interpolationMode =
                    System.Drawing.Drawing2D.InterpolationMode.High;

                Graphics g = Graphics.FromImage(_image);
                g.InterpolationMode = interpolationMode;

                Image img = GenerateTileImage(x, y);
                g.DrawImage(img, (int)(x * roomWidth), (int)(y * roomHeight),
                            (int)(roomWidth), (int)(roomHeight));

                g.Dispose();

                QueueDrawArea(x * roomWidth, y * roomHeight, roomWidth, roomHeight);

                idleX++;
                if (idleX >= _map.MapWidth)
                {
                    idleY++;
                    idleX = 0;
                }

                return(true);
            };

            int width  = (int)(_map.RoomWidth * _map.MapWidth * 16 * scale);
            int height = (int)(_map.RoomHeight * _map.MapHeight * 16 * scale);

            _image = new Bitmap(width, height);

            SetSizeRequest(width, height);
            idleX = 0;
            idleY = 0;
            var handler = new GLib.IdleHandler(OnIdleGenerateImage);

            GLib.Idle.Remove(handler);
            GLib.Idle.Add(handler);
        }
Ejemplo n.º 3
0
        void DrawCyTitle(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, RectangleF[] rtfs, RectangleF rtCyTitle, int _charFullWidth, int _charWidth)
        {
            StringFormat _sf = new StringFormat();

            _sf.Alignment     = StringAlignment.Center;
            _sf.LineAlignment = StringAlignment.Center;
            Font     CyTitleFont = new Font("楷体_GB2312", 9F);
            Graphics g           = e.Graphics;

            System.Drawing.Text.TextRenderingHint      trh = g.TextRenderingHint;
            System.Drawing.Drawing2D.InterpolationMode im  = g.InterpolationMode;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            RectangleF rtf       = new RectangleF(rtCyTitle.Right - _charWidth, rtCyTitle.Y, _charWidth, rtCyTitle.Height);
            int        maxLength = rtfs.Length > CyEditor.CYTITLE.Length ? CyEditor.CYTITLE.Length : rtfs.Length;//用小的
            int        j         = 0;

            for (int i = CyEditor.CYTITLE.Length - 1; i >= 0; i--)
            {
                if (j >= maxLength)
                {
                    break;
                }
                string s = CyEditor.CYTITLE.Substring(i, 1);
                g.DrawString(s, CyTitleFont, new SolidBrush(Color.Green), rtf, _sf);
                rtf.Offset(-_charFullWidth, 0);
                j++;
            }
            g.TextRenderingHint = trh;
            g.InterpolationMode = im;
            DrawCyLine(g, rtCyTitle.X, rtCyTitle.Y, rtCyTitle.Width, rtCyTitle.Height, _charFullWidth);
        }
Ejemplo n.º 4
0
        // from https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp
        public static Bitmap ResizeImage(this Image image, int width, int height,
                                         System.Drawing.Drawing2D.InterpolationMode interm = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                                         )
        {
            if (width <= 0)
            {
                width = image.Width;
            }
            if (height <= 0)
            {
                height = image.Height;
            }

            var destRect  = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode    = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode  = interm;
                graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                using (var wrapMode = new System.Drawing.Imaging.ImageAttributes())
                {
                    wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return(destImage);
        }
Ejemplo n.º 5
0
        static public InterpolationMode ToInterpolationMode(this System.Drawing.Drawing2D.InterpolationMode mode)
        {
            switch (mode)
            {
            case System.Drawing.Drawing2D.InterpolationMode.Low:
                return(InterpolationMode.Low);

            case System.Drawing.Drawing2D.InterpolationMode.High:
                return(InterpolationMode.High);

            case System.Drawing.Drawing2D.InterpolationMode.Bicubic:
                return(InterpolationMode.Bicubic);

            case System.Drawing.Drawing2D.InterpolationMode.Bilinear:
                return(InterpolationMode.Bilinear);

            case System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor:
                return(InterpolationMode.NearestNeighbor);

            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:
                return(InterpolationMode.HighQualityBicubic);

            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear:
                return(InterpolationMode.HighQualityBilinear);

            default:
                return(InterpolationMode.Default);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 绘制元素
 /// </summary>
 /// <param name="g">图形绘制对象</param>
 /// <param name="ClipRectangle">剪切矩形</param>
 protected virtual void DrawContent(DomParagraphFlagElement eof, DocumentPaintEventArgs args)
 {
     if (_ParagraphEOFIcon == null)
     {
         _ParagraphEOFIcon = (System.Drawing.Bitmap)WriterResources.paragrapheof.Clone();
         _ParagraphEOFIcon.MakeTransparent(Color.White);
     }
     if (this.Document.Options.ViewOptions.ShowParagraphFlag &&
         args.RenderStyle == DocumentRenderStyle.Paint)
     {
         System.Drawing.RectangleF rect = eof.AbsBounds;
         if (args.RenderStyle == DocumentRenderStyle.Paint)
         {
             System.Drawing.Size size = _ParagraphEOFIcon.Size;
             size = this.Document.PixelToDocumentUnit(size);
             System.Drawing.Drawing2D.InterpolationMode back
                 = args.Graphics.InterpolationMode;
             args.Graphics.InterpolationMode
                 = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
             args.Graphics.DrawImage(
                 _ParagraphEOFIcon,
                 rect.Left,
                 rect.Bottom - size.Height);
             args.Graphics.InterpolationMode = back;
         }
     }
 }
 protected override void OnPaint(PaintEventArgs paintEventArgs)
 {
     System.Drawing.Drawing2D.InterpolationMode OldInterpolationMode = paintEventArgs.Graphics.InterpolationMode;
     paintEventArgs.Graphics.InterpolationMode = InterpolationMode;
     base.OnPaint(paintEventArgs);
     paintEventArgs.Graphics.InterpolationMode = OldInterpolationMode;
 }
Ejemplo n.º 8
0
        private static Bitmap Resize(this Bitmap source, Int32 width, Int32 height, System.Drawing.Drawing2D.InterpolationMode mode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor)
        {
            var result = new Bitmap(width, height);

            using (var g = Graphics.FromImage(result))
            {
                g.InterpolationMode = mode;
                g.DrawImage(source, 0, 0, width, height);
            }
            return(result);
        }
Ejemplo n.º 9
0
        private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight,
                                  System.Drawing.Drawing2D.InterpolationMode interpMode)
        {
            /*Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight, PixelFormat.Format24bppRgb);
             * scaledImage.SetResolution(96.0f, 96.0f);
             *
             * Graphics grPhoto = Graphics.FromImage(scaledImage);
             * grPhoto.InterpolationMode = interpMode;
             *
             * grPhoto.DrawImage(srcImage,
             *  new Rectangle(0, 0, destWidth, destHeight),
             *  new Rectangle(0, 0, srcImage.Width, srcImage.Height),
             *  GraphicsUnit.Pixel);
             *
             * grPhoto.Dispose();
             * return scaledImage;*/
            int sourceWidth  = srcImage.Width;
            int sourceHeight = srcImage.Height;
            int sourceX      = 0;
            int sourceY      = 0;

            int destX = 0;
            int destY = 0;

            if (srcImage.PixelFormat == PixelFormat.Format32bppArgb)
            {
                for (int y = 0; y < srcImage.Height; y++)
                {
                    for (int x = 0; x < srcImage.Width; x++)
                    {
                        Color c = srcImage.GetPixel(x, y);
                        srcImage.SetPixel(x, y, Color.FromArgb(255, c.R, c.G, c.B));
                    }
                }
            }

            Bitmap scaledImage = new Bitmap(destWidth, destHeight,
                                            PixelFormat.Format24bppRgb);

            scaledImage.SetResolution(96.0f, 96.0f);

            Graphics grPhoto = Graphics.FromImage(scaledImage);

            grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

            grPhoto.DrawImage(srcImage,
                              new Rectangle(destX, destY, destWidth, destHeight),
                              new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                              GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return(scaledImage);
        }
Ejemplo n.º 10
0
        public void Downscale(int newwidth, int newheight, System.Drawing.Drawing2D.InterpolationMode mode)
        {
            float[] newvalues = new float[newwidth * newheight];

            // Nearest neighbour sampling
            for (int y = 0; y < newheight; ++y)
            {
                for (int x = 0; x < newwidth; ++x)
                {
                    float src_xf = (float)x * (float)width / (float)newwidth;
                    int   src_xi = (int)src_xf;
                    if (src_xf - (float)src_xi >= 0.5f)
                    {
                        ++src_xi;
                    }

                    float src_yf = (float)y * (float)height / (float)newheight;
                    int   src_yi = (int)src_yf;
                    if (src_yf - (float)src_yi >= 0.5f)
                    {
                        ++src_yi;
                    }

                    newvalues[y * newwidth + x] = values[src_yi * width + src_xi];
                }
            }

            /*// Linear sampling
             * for(int y = 0; y < newheight; ++y)
             *      for(int x = 0; x < newwidth; ++x)
             *      {
             *              float src_xf = (float)x * (float)width / (float)newwidth;
             *              int src_xi = (int)src_xf;
             *              src_xf = src_xf - (float)src_xi;
             *
             *              float src_yf = (float)y * (float)height / (float)newheight;
             *              int src_yi = (int)src_yf;
             *              src_yf = src_yf - (float)src_yi;
             *
             *              newvalues[y * newwidth + x] = values[src_yi * width + src_xi] * src_xf * src_yf;
             *              newvalues[y * newwidth + x] += values[src_yi * width + src_xi + 1] * (1.0f - src_xf) * src_yf;
             *              newvalues[y * newwidth + x] += values[++src_yi * width + src_xi] * src_xf * (1.0f - src_yf);
             *              newvalues[y * newwidth + x] += values[src_yi * width + src_xi + 1] * (1.0f - src_xf) * (1.0f - src_yf);
             *      }*/

            width  = newwidth;
            height = newheight;
            values = null;
            values = newvalues;
        }
Ejemplo n.º 11
0
        void SaveResizedBMPImage(string source, string destination, System.Drawing.Drawing2D.InterpolationMode interpolation, int width, int height)
        {
            Bitmap bsource = new Bitmap(source);
            Bitmap bitmap  = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.InterpolationMode = interpolation;
                g.DrawImage(bsource, 0, 0, width, height);
            }
            bitmap.Save(destination, System.Drawing.Imaging.ImageFormat.Bmp);
            bitmap.Dispose();
            bsource.Dispose();
        }
Ejemplo n.º 12
0
        public void Downscale(int newwidth, int newheight, System.Drawing.Drawing2D.InterpolationMode mode)
        {
            Bitmap originalBmp = bmp;

            bmp = new Bitmap(newwidth, newheight, originalBmp.PixelFormat);
            Graphics gfx = Graphics.FromImage(bmp);

            gfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            gfx.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            gfx.InterpolationMode  = mode;
            gfx.DrawImage(originalBmp, new Rectangle(0, 0, newwidth, newheight));
            gfx.Flush();
            originalBmp.Dispose();
            originalBmp = null;
        }
        /// <summary>
        /// Resizes a bitmap
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="destW"></param>
        /// <param name="destH"></param>
        /// <param name="maintainProportions"></param>
        /// <param name="interpolationMode"></param>
        /// <param name="smoothingMode"></param>
        /// <returns></returns>
        public static Bitmap Resize(this Bitmap bitmap, int destW, int destH, bool maintainProportions = true,
                                    System.Drawing.Drawing2D.InterpolationMode interpolationMode       =
                                    System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic,
                                    System.Drawing.Drawing2D.SmoothingMode smoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias)
        {
            var output = new System.Drawing.Bitmap(destW, destH);

            if (!maintainProportions)
            {
                using (var g = System.Drawing.Graphics.FromImage(output))
                {
                    g.InterpolationMode = interpolationMode;
                    g.SmoothingMode     = smoothingMode;
                    g.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    g.DrawImage(bitmap, 0, 0, destW, destH);
                }
            }
            else
            {
                //get the scaling ratio
                var scaleRatio = Math.Min((double)destW / (double)bitmap.Width,
                                          (double)destH / (double)bitmap.Height);

                //resize image appropriately
                bitmap = Resize(bitmap, (int)(bitmap.Width * scaleRatio), (int)(bitmap.Height * scaleRatio), false,
                                interpolationMode, smoothingMode);

                //and paint it in the middle of destination canvas
                using (var g = System.Drawing.Graphics.FromImage(output))
                {
                    g.InterpolationMode = interpolationMode;
                    g.SmoothingMode     = smoothingMode;
                    g.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    g.DrawImage(
                        bitmap,
                        (destW - bitmap.Width) / 2,
                        (destH - bitmap.Height) / 2,
                        bitmap.Width,
                        bitmap.Height
                        );
                }
            }

            return(output);
        }
Ejemplo n.º 14
0
        private Bitmap ScaleImage(Bitmap srcImage, int destWidth, int destHeight,
                                  System.Drawing.Drawing2D.InterpolationMode interpMode)
        {
            Bitmap scaledImage = new Bitmap(srcImage, destWidth, destHeight);

            scaledImage.SetResolution(96.0f, 96.0f);

            Graphics grPhoto = Graphics.FromImage(scaledImage);

            grPhoto.InterpolationMode = interpMode;

            grPhoto.DrawImage(srcImage,
                              new Rectangle(0, 0, destWidth, destHeight),
                              new Rectangle(0, 0, srcImage.Width, srcImage.Height),
                              GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return(scaledImage);
        }
Ejemplo n.º 15
0
        void CreateResizedImage()
        {
            if (!File.Exists(ImagePath) || Convert.ToInt32(TextBox_Width.Text) == 0 || Convert.ToInt32(TextBox_Height.Text) == 0)
            {
                return;
            }
            if (File.Exists(ImageResizedPath))
            {
                File.Delete(ImageResizedPath);
            }
            ImageResizedPath = Path.GetTempPath() + Guid.NewGuid() + ".bmp";
            System.Drawing.Drawing2D.InterpolationMode interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            if (ComboBox_Interpolation != null)
            {
                switch (ComboBox_Interpolation.SelectedIndex)
                {
                case 0:
                    interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    break;

                case 1:
                    interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                    break;

                case 2:
                    interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    break;
                }
            }
            SaveResizedBMPImage(ImagePath, ImageResizedPath, interpolation, Convert.ToInt32(TextBox_Width.Text), Convert.ToInt32(TextBox_Height.Text));

            ImageResizedBitmap = new BitmapImage();
            ImageResizedBitmap.BeginInit();
            ImageResizedBitmap.UriSource   = new Uri(ImageResizedPath);
            ImageResizedBitmap.CacheOption = BitmapCacheOption.OnLoad;
            ImageResizedBitmap.EndInit();
            Image_PictureBox.Source = ImageResizedBitmap;
            Image_PictureBox.Width  = ImageResizedBitmap.PixelWidth;
            Image_PictureBox.Height = ImageResizedBitmap.PixelHeight;
        }
Ejemplo n.º 16
0
        public static Bitmap kResizeImage(Bitmap bmp, int newW, int newH, System.Drawing.Drawing2D.InterpolationMode currnetMode)
        {
            //插值缩放算法
            try
            {
                Bitmap   b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的质量
                //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                g.InterpolationMode = currnetMode;

                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();

                return(b);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 17
0
Archivo: xlator.cs Proyecto: zhabis/nfx
        internal static InterpolationMode xlat(System.Drawing.Drawing2D.InterpolationMode mode)
        {
            switch (mode)
            {
            case System.Drawing.Drawing2D.InterpolationMode.Low:                 return(InterpolationMode.Low);

            case System.Drawing.Drawing2D.InterpolationMode.High:                return(InterpolationMode.High);

            case System.Drawing.Drawing2D.InterpolationMode.Bilinear:            return(InterpolationMode.Bilinear);

            case System.Drawing.Drawing2D.InterpolationMode.Bicubic:             return(InterpolationMode.Bicubic);

            case System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor:     return(InterpolationMode.NearestNeighbor);

            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear: return(InterpolationMode.HQBilinear);

            case System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:  return(InterpolationMode.HQBicubic);

            //Invalid = -1,
            //Default,
            default: return(InterpolationMode.Default);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// PictureBoxからGraphicsオブジェクトの作成
        /// </summary>
        /// <param name="pic">作成する対象のPictureBox</param>
        /// <param name="graphics">前回のGraphicsオブジェクト</param>
        /// <param name="mode">補間モード</param>
        /// <returns>作成したGraphicsオブジェクト</returns>
        public static void CreateGraphics(this PictureBox pic, ref Graphics graphics, System.Drawing.Drawing2D.InterpolationMode mode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor)
        {
            if ((pic.Width == 0) || (pic.Height == 0))
            {
                return;
            }

            if (graphics != null)
            {
                graphics.Dispose();
            }

            var bmp = pic.Image as Bitmap;

            if (bmp != null)
            {
                bmp.Dispose();
            }

            bmp = new Bitmap(pic.Width, pic.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            // BitmapクラスオブジェクトをPictureBocのImageへ
            pic.Image = bmp;

            graphics = Graphics.FromImage(bmp);
            graphics.InterpolationMode = mode;
        }
Ejemplo n.º 19
0
        private Bitmap ResizeBitmap(Bitmap original, int width, int height, System.Drawing.Drawing2D.InterpolationMode interpolationMode)
        {
            Bitmap   bmpResize;
            Bitmap   bmpResizeColor;
            Graphics graphics = null;

            try
            {
                System.Drawing.Imaging.PixelFormat pf = original.PixelFormat;

                if (original.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                {
                    // モノクロの時は仮に24bitとする
                    pf = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
                }

                bmpResizeColor = new Bitmap(width, height, pf);
                var dstRect = new RectangleF(0, 0, width, height);
                var srcRect = new RectangleF(-0.5f, -0.5f, original.Width, original.Height);
                graphics = Graphics.FromImage(bmpResizeColor);
                graphics.Clear(Color.Transparent);
                graphics.InterpolationMode = interpolationMode;
                graphics.DrawImage(original, dstRect, srcRect, GraphicsUnit.Pixel);
            }
            finally
            {
                if (graphics != null)
                {
                    graphics.Dispose();
                }
            }

            if (original.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                // モノクロ画像のとき、24bit→8bitへ変換

                // モノクロBitmapを確保
                bmpResize = new Bitmap(
                    bmpResizeColor.Width,
                    bmpResizeColor.Height,
                    System.Drawing.Imaging.PixelFormat.Format8bppIndexed
                    );

                var pal = bmpResize.Palette;
                for (int i = 0; i < bmpResize.Palette.Entries.Length; i++)
                {
                    pal.Entries[i] = original.Palette.Entries[i];
                }
                bmpResize.Palette = pal;

                // カラー画像のポインタへアクセス
                var bmpDataColor = bmpResizeColor.LockBits(
                    new Rectangle(0, 0, bmpResizeColor.Width, bmpResizeColor.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    bmpResizeColor.PixelFormat
                    );

                // モノクロ画像のポインタへアクセス
                var bmpDataMono = bmpResize.LockBits(
                    new Rectangle(0, 0, bmpResize.Width, bmpResize.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    bmpResize.PixelFormat
                    );

                int colorStride = bmpDataColor.Stride;
                int monoStride  = bmpDataMono.Stride;

                unsafe
                {
                    var pColor = (byte *)bmpDataColor.Scan0;
                    var pMono  = (byte *)bmpDataMono.Scan0;
                    for (int y = 0; y < bmpDataColor.Height; y++)
                    {
                        for (int x = 0; x < bmpDataColor.Width; x++)
                        {
                            // R,G,B同じ値のため、Bの値を代表してモノクロデータへ代入
                            pMono[x + y * monoStride] = pColor[x * 3 + y * colorStride];
                        }
                    }
                }

                bmpResize.UnlockBits(bmpDataMono);
                bmpResizeColor.UnlockBits(bmpDataColor);

                // 解放
                bmpResizeColor.Dispose();
            }
            else
            {
                // カラー画像のとき
                bmpResize = bmpResizeColor;
            }

            return(bmpResize);
        }
Ejemplo n.º 20
0
 public PictureBoxWithInterpolationMode(System.Drawing.Drawing2D.InterpolationMode mode)
 {
     this.InterpolationMode = mode;
     this.DoubleBuffered    = true;
 }
Ejemplo n.º 21
0
        private void Button_OpenDirectory_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog browserDialog = new System.Windows.Forms.FolderBrowserDialog
            {
                Description = "选择要进行批处理的文件夹。"
            };
            if (browserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(browserDialog.SelectedPath);

            string[] extension = { ".bmp", ".jpg", ".png", ".gif" };
            int      count     = 0;

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (extension.Contains(file.Extension.ToLower()))
                {
                    count++;
                }
            }
            if (MessageBox.Show(string.Format("将对{0}的{1}张图片进行批量转换,准备好了吗?", browserDialog.SelectedPath, count), this.Title, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
            {
                return;
            }

            Directory.CreateDirectory(browserDialog.SelectedPath + "\\mozjpeg-converted");

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (!extension.Contains(file.Extension.ToLower()))
                {
                    continue;
                }

                GC.Collect();

                string BatchImagePath        = Path.GetTempPath() + "tmp" + GetRandomString(4) + ".bmp";
                string BatchImageResizedPath = Path.GetTempPath() + "tmp" + GetRandomString(4) + ".bmp";
                System.Drawing.Drawing2D.InterpolationMode interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                if (ComboBox_Interpolation != null)
                {
                    switch (ComboBox_Interpolation.SelectedIndex)
                    {
                    case 0:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        break;

                    case 1:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        break;

                    case 2:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                        break;
                    }
                }

                SaveBMPImage(file.FullName, BatchImagePath);
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource   = new Uri(BatchImagePath);
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.EndInit();
                int    width  = bitmap.PixelWidth;
                int    height = bitmap.PixelHeight;
                double ratio  = (double)width / (double)height;
                switch (ComboBox_Batch.SelectedIndex)
                {
                case 0:
                    width  = (int)Math.Round(width * Convert.ToDouble(TextBox_Scale.Text) / 100);
                    height = (int)Math.Round(height * Convert.ToDouble(TextBox_Scale.Text) / 100);
                    break;

                case 1:
                    width  = Convert.ToInt32(TextBox_Width.Text);
                    height = (int)Math.Round(width / ratio);
                    break;

                case 2:
                    height = Convert.ToInt32(TextBox_Height.Text);
                    width  = (int)Math.Round(height * ratio);
                    break;
                }
                SaveResizedBMPImage(BatchImagePath, BatchImageResizedPath, interpolation, width, height);

                string sample = "";
                string image  = " -optimize ";

                switch (ComboBox_SampleFormat.SelectedIndex)
                {
                case 0:     //yuv420
                    sample = " -sample 2x2 ";
                    break;

                case 1:     //yuv422
                    sample = " -sample 2x1 ";
                    break;

                case 2:     //yuv444
                    sample = " -sample 1x1 ";
                    break;

                case 3:     //rgb
                    sample = " -rgb ";
                    break;
                }
                switch (ComboBox_ImageFormat.SelectedIndex)
                {
                case 0:     //baseline
                    image += "-baseline";
                    break;

                case 1:     //progressive grayscale
                    image += "-progressive -greyscale";
                    break;

                case 2:     //progressive non-interleaved
                    image += "-progressive -dc-scan-opt 0";
                    break;

                case 3:     //progressive interleaved
                    image += "-progressive -dc-scan-opt 2";
                    break;
                }

                Process process = new Process();
                process.StartInfo.FileName  = CjpegPath;
                process.StartInfo.Arguments = "-quality " + Slider_Quality.Value.ToString() +
                                              " -smooth " + Slider_Smooth.Value +
                                              image +
                                              sample +
                                              " -quant-table " + ComboBox_QuantTable.SelectedIndex +
                                              " -outfile \"" + browserDialog.SelectedPath + "\\mozjpeg-converted\\" + Path.GetFileNameWithoutExtension(file.Name) +
                                              ".jpg\" \"" +
                                              BatchImageResizedPath + "\"";
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardInput  = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.CreateNoWindow         = true;

                process.Start();
                process.WaitForExit();
                process.Close();

                File.Delete(BatchImagePath);
                File.Delete(BatchImageResizedPath);
            }

            MessageBox.Show("批量转换完成!\n转换后的文件保存在转换目录的mozjpeg-converted文件夹中。", this.Title, MessageBoxButton.OK, MessageBoxImage.Information);
        }
Ejemplo n.º 22
0
        private void Button_OpenDirectory_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog browserDialog = new System.Windows.Forms.FolderBrowserDialog
            {
                Description = (string)TryFindResource("BatchSelectFolder")
            };
            if (browserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(browserDialog.SelectedPath);

            string[] extension = { ".bmp", ".jpg", ".jpeg", ".png", ".gif", "*.webp" };
            int      count     = 0;

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (extension.Contains(file.Extension.ToLower()))
                {
                    count++;
                }
            }
            if (MessageBox.Show(string.Format((string)TryFindResource("BatchConfirm"), browserDialog.SelectedPath, count), this.Title, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
            {
                return;
            }

            Directory.CreateDirectory(browserDialog.SelectedPath + "\\mozjpeg-converted");

            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                if (!extension.Contains(file.Extension.ToLower()))
                {
                    continue;
                }

                GC.Collect();

                string BatchImagePath        = Path.GetTempPath() + Guid.NewGuid() + ".bmp";
                string BatchImageResizedPath = Path.GetTempPath() + Guid.NewGuid() + ".bmp";
                System.Drawing.Drawing2D.InterpolationMode interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                if (ComboBox_Interpolation != null)
                {
                    switch (ComboBox_Interpolation.SelectedIndex)
                    {
                    case 0:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        break;

                    case 1:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        break;

                    case 2:
                        interpolation = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                        break;
                    }
                }

                SaveBMPImage(file.FullName, BatchImagePath);
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource   = new Uri(BatchImagePath);
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.EndInit();
                int    width  = bitmap.PixelWidth;
                int    height = bitmap.PixelHeight;
                double ratio  = (double)width / (double)height;
                switch (ComboBox_Batch.SelectedIndex)
                {
                case 0:
                    width  = (int)Math.Round(width * Convert.ToDouble(TextBox_Scale.Text) / 100);
                    height = (int)Math.Round(height * Convert.ToDouble(TextBox_Scale.Text) / 100);
                    break;

                case 1:
                    width  = Convert.ToInt32(TextBox_Width.Text);
                    height = (int)Math.Round(width / ratio);
                    break;

                case 2:
                    height = Convert.ToInt32(TextBox_Height.Text);
                    width  = (int)Math.Round(height * ratio);
                    break;
                }
                SaveResizedBMPImage(BatchImagePath, BatchImageResizedPath, interpolation, width, height);

                switch (ComboBox_Tool.SelectedIndex)
                {
                case 0:     // mozjpeg
                    EncodeImage(BatchImageResizedPath, browserDialog.SelectedPath + "\\mozjpeg-converted\\" + Path.GetFileNameWithoutExtension(file.Name) + ".jpg");
                    break;

                case 1:     // libwebp
                    EncodeImage(BatchImageResizedPath, browserDialog.SelectedPath + "\\mozjpeg-converted\\" + Path.GetFileNameWithoutExtension(file.Name) + ".webp");
                    break;
                }

                File.Delete(BatchImagePath);
                File.Delete(BatchImageResizedPath);
            }

            MessageBox.Show((string)TryFindResource("BatchComplete"), this.Title, MessageBoxButton.OK, MessageBoxImage.Information);
            browserDialog.Dispose();
        }
Ejemplo n.º 23
0
 public GamePreviewBox()
 {
     DoubleBuffered    = true;
     InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
 }
Ejemplo n.º 24
0
 public EtoCell()
 {
     InterpolationMode = sd.Drawing2D.InterpolationMode.HighQualityBicubic;
 }
        /// <summary>
        /// Creates a new image and lays the list of images in the cells of a grid.
        /// Fills the grid is filled left to right, top to bottom.
        /// Images will be resized to the cell size.
        /// Final image will be as high as necessary depending on the amount of images.
        /// </summary>
        /// <param name="images"></param>
        /// <param name="imageWidth">Width of grid, with no padding.</param>
        /// <param name="cellWidth">Width of a cell, with no padding.</param>
        /// <param name="cellHeight">Height of a cell, with no padding.</param>
        /// <param name="interpolationMode">Quality of the interpolation used for resizing the images.</param>
        /// <returns>A single image with the Grid as described before.</returns>
        private Image MergeImageInGrid(List <Image> images, int imageWidth, int cellWidth, int cellHeight, System.Drawing.Drawing2D.InterpolationMode interpolationMode)
        {
            int cols = imageWidth / cellWidth;

            //Integer division of doing "ceiling"
            int rows = (images.Count + cols - 1) / cols;

            int gridHeight = rows * cellHeight;

            Image grid = new Bitmap(imageWidth, gridHeight);

            using (Graphics graphics = Graphics.FromImage(grid)) {
                graphics.Clear(Color.Black);
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode  = interpolationMode;
                graphics.CompositingMode    = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.None;

                for (int i = 0; i < images.Count; ++i)
                {
                    //x = (i % cols) * cellWidth, this makes it wrap on the row
                    //y =  (i / cols) * cellHeight, this makes it go down a row every time a full row is drawn
                    graphics.DrawImage(images[i], (i % cols) * cellWidth, (i / cols) * cellHeight);
                }
            }

            return(grid);
        }