C# 绘画 作画 无闪烁 draw graphics (C#无闪烁画图一)

在WINFORM上,点一下按钮,会出现一个图标,和几个字,效果如下:

这里是全部的代码:

namespace WinFormDraw
{
    public partial class Form1 : Form
    {
        public Bitmap bmp = new Bitmap(200, 200);//先新建一个bmp对象,200x200  
        public Graphics graphics = null;

        Image png06 = Image.FromFile(@"v.png");   

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            graphics = Graphics.FromImage(bmp); //拿到bmp的graphics画图对象
            graphics.DrawImage(png06, 50, 50);//画PNG
            graphics.DrawString("村民", new Font("宋体", 20, FontStyle.Bold), Brushes.Red, new PointF(29, 29));//写字
          
            this.CreateGraphics().DrawImage(bmp, 0, 0);  //显示这张BMP
        }
    }
}

代码的详细说明如下:

 

一、新建一个bmp对象,大小是200*200 

 public Bitmap bmp = new Bitmap(200, 200);

二、准备一张图片,把它和项目编译程序放一个目录

 

我准备了一张PNG图片,20*20像素,把它和项目编译程序放一个目录:

用这行代码,我们加载它:

  Image png06 = Image.FromFile(@"v.png");   

三、拿到bmp对象的画图graphics对象

public Graphics graphics = null;

graphics = Graphics.FromImage(bmp); //拿到bmp的graphics画图对象

四、我们在graphics对象上画图标和画写字

graphics.DrawImage(png06, 50, 50);//画PNG
graphics.DrawString("村民", new Font("宋体", 20, FontStyle.Bold), Brushes.Red, new PointF(29, 29));//写字

五、最后我们在WINFORM窗体上把BMP显示出来

this.CreateGraphics().DrawImage(bmp, 0, 0);  //显示这张BMP

最后说明:

先在graphics对象上画我们的图像和文字,最后再把一幅完整的BMP一下显示出来,这样可以避免图像内容更新带来的闪烁。

后面的DEMO中出现了动画:C# WINFORM 控件动画无闪烁的例子DEMO(C#无闪烁画图二)

 

 

 

AppWizard has created this drawGraphics application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your drawGraphics application. drawGraphics.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. drawGraphics.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CDrawGraphicsApp application class. drawGraphics.cpp This is the main application source file that contains the application class CDrawGraphicsApp. drawGraphics.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. drawGraphics.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. resdrawGraphics.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file drawGraphics.rc. resdrawGraphics.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘欣的博客

你将成为第一个打赏博主的人!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值