zoukankan      html  css  js  c++  java
  • winform双缓冲, 从内存画图到到picturebox

     base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

    private Image DrawImage(string strFilePath)

            {
                try
                {
                    Bitmap bitmap = new Bitmap(strFilePath);
                    //如果原图片是索引像素格式之列的,则需要转换
                    if (IsPixelFormatIndexed(bitmap.PixelFormat))
                    {
                        Bitmap bmp = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                            g.DrawImage(bitmap, 00);
                            Pen pen = new Pen(Color.Red, 3);
                            for (int i = 0; i < bitmap.Width; i += bitmap.Width / 4)
                            {
                                g.DrawLine(pen, i, 0, i, bitmap.Height);
                            }
                            for (int i = 0; i < bitmap.Height; i += bitmap.Height / 4)
                            {
                                g.DrawLine(pen, 0, i, bitmap.Width, i);
                            }
                        }
                    }
                    else
                    {
                        Graphics g = Graphics.FromImage(bitmap);
                        g.DrawImage(bitmap, bitmap.Width, bitmap.Height);
                        Pen pen = new Pen(Color.Red, 3);
                        for (int i = 0; i < bitmap.Width; i += bitmap.Width / 4)
                        {
                            g.DrawLine(pen, i, 0, i, bitmap.Height);
                        }
                        for (int i = 0; i < bitmap.Height; i += bitmap.Height / 4)
                        {
                            g.DrawLine(pen, 0, i, bitmap.Width, i);
                        }
                    }
                    return bitmap;
                }
                catch { return null; }
            }

       /// <summary>
            
    /// 会产生graphics异常的PixelFormat
            
    /// </summary>
            private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
                PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
                PixelFormat.Format8bppIndexed
            };
            /// <summary>
            
    /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
            
    /// </summary>
            
    /// <param name="imgPixelFormat">原图片的PixelFormat</param>
            
    /// <returns></returns>
            private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
            {
                foreach (PixelFormat pf in indexedPixelFormats)
                {
                    if (pf.Equals(imgPixelFormat)) return true;
                }

                return false;

            }

    下面这个函数从内存画图到picbox, 因为有些图片是索引色, 所以先鉴定了下图片. 画图时用线画了格子. 不用时可以去了.

  • 相关阅读:
    python3 与dict相关的魔法方法。使用于二叉搜索树的类中
    笔记:nestjs学习基础(1)
    ES6 --(10)class使用、class继承
    redux --(1)核心概念(stateaction educer)、三大原则、todolist with redux
    antV--G2 学习
    react源代码重点难点分析
    jQuery-ui源代码重点难点分析
    webuploader上传插件源代码重点难点分析
    破解jQuery Deferred()异步执行的神秘世界
    ueditor源代码重点难点分析
  • 原文地址:https://www.cnblogs.com/barrysgy/p/2276971.html
Copyright © 2011-2022 走看看