• C#中Bitmap类实现对图像操作的一些方法


    导入以下两个包:

        System.Drawing;

          System.Drawing.Imaging;

    建产对象:

         Bitmap bm = new Bitmap("c:/1.bmp");

    缩放:

         Bitmap bm1 = new Bitmap(bm,width,height);

    格式转换:

         bm.save("c:/1.jpg",ImageFromat.Jpeg);

         bm1.Save("c:/1.gif", ImageFormat.Gif);

    剪切一个区域:

         //剪切大小

        int cutwidth;

          int cutheight;

         Graphics g;

         //以大小为剪切大小,像素格式为32位RGB创建一个位图对像

         Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;

        //定义一个区域

         Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);

         //要绘制到的位图

         g = Graphics.FromImage(bm1);

         //将bm内rg所指定的区域绘制到bm1

         g.DrawImage(bm,rg)

    ============================================
    C#Bitmap代替另一个Bitmap的某部分
    Bitmap bm = new Bitmap(宽度, 高度);// 新建一个 Bitmap 位图 
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根据新建的 Bitmap 位图,创建画布 
    g.Clear(System.Drawing.Color.Black);// 使用黑色重置画布 
    g.DrawImage(源位图, ......); // 绘制“源位图”,后面有若干参数控制大小、坐标等等功能。
    ==================================================
    C# 图片处理之:旋转图片任意角度
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客     ///<summary>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        /// 任意角度旋转
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        ///</summary>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        ///<param name="bmp">原始图Bitmap</param>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        ///<param name="angle">旋转角度</param>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        ///<param name="bkColor">背景色</param>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        ///<returns>输出Bitmap</returns>
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客        publicstatic Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客       {
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客            int w = bmp.Width +2;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客            int h = bmp.Height +2;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             PixelFormat pf;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客            if (bkColor == Color.Transparent)
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客           {
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客                 pf = PixelFormat.Format32bppArgb;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             }
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客            else
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客           {
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客                 pf = bmp.PixelFormat;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             }
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             Bitmap tmp =new Bitmap(w, h, pf);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             Graphics g = Graphics.FromImage(tmp);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.Clear(bkColor);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.DrawImageUnscaled(bmp, 1, 1);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.Dispose();
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             GraphicsPath path =new GraphicsPath();
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             path.AddRectangle(new RectangleF(0f, 0f, w, h));
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             Matrix mtrx =new Matrix();
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             mtrx.Rotate(angle);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             RectangleF rct = path.GetBounds(mtrx);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             Bitmap dst =new Bitmap((int)rct.Width, (int)rct.Height, pf);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g = Graphics.FromImage(dst);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.Clear(bkColor);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.TranslateTransform(-rct.X, -rct.Y);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.RotateTransform(angle);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.InterpolationMode = InterpolationMode.HighQualityBilinear;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.DrawImageUnscaled(tmp, 0, 0);
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             g.Dispose();
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客             tmp.Dispose();
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客            return dst;
    C中Bitmap类实现对图像操作的一些方法 - yishimengying - yishimengying的博客         }
    -------------------------
    这张图由以下代码产生。左面的是用DrawString函数直接绘制在Form上的,右面的是在Bitmap对象中绘制好后,DrawImage到窗体上的。可以看到文本显示效果非常不同。因为我想使用双缓冲,把大量文本先绘制在BitMap上,再让它显示出来,但是两种显示效果不一样是无法容忍的。请问,这是为什么?怎样让两种方法绘制的文本显示效果一模一样即使换了字体,两种方法的显示效果仍然不一致。
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        
        bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
        Graphics gBmp = Graphics.FromImage(bmp);//内存位图
        Graphics gForm = e.Graphics;//Form
        for (int i = 1; i < 20; i++)
        {
            System.Drawing.Font f = new Font("宋体", i, FontStyle.Regular);
            gForm.DrawString("this is a test", f, Brushes.Black, new PointF(10f, i*Font.Height));
            gBmp.DrawString("this is a test", f, Brushes.Black, new PointF(0f, i * Font.Height));
        }
        gForm.DrawImage(bmp, new Point(200, 0));
  • 相关阅读:
    mysql max_allowed_packet过小导致的prepare失败
    linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解
    mysql metadata lock锁
    velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
    mysql 5.7.15发布
    mysql 5.6.33发布
    2016年09月编程语言排行榜
    postgresql 9.6 rc1发布
    www.97top10.com--做最好的技术交流网站
    nginx/ajax跨子域请求的两种现代方法以及403解决
  • 原文地址:https://www.cnblogs.com/zcm123/p/6535244.html
Copyright © 2020-2023  润新知