| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 748 人关注过本帖
标题:C# GDI+ 出现的令人诧异的问题,来看看,或许你也从未遇到过
只看楼主 加入收藏
vachul
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-10
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
C# GDI+ 出现的令人诧异的问题,来看看,或许你也从未遇到过
Form1:
C# code:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace yanshi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
                Thread th = new Thread(new ThreadStart(dr));
                th.Start();
        }

        public void dr()
        {            
            int i = 0;
            Graphics g = this.CreateGraphics();
            Color col1 = Color.White;
            Color col2 = Color.Black;
            SolidBrush brus1 = new SolidBrush(col1);
            SolidBrush brus2 = new SolidBrush(col2);
            Rectangle rect = new Rectangle(0, 0, 0, 0);
            rect.Location = new Point(100, 400);
            rect.Width = 200;      //Set rect's width
            rect.Height = 200;     //set rect's height
            do
            {
                if (i % 2 == 0)
                {
                    g.FillRectangle(brus1, rect);//Drawing white for high voltage                     
                }
                else
                {
                    g.FillRectangle(brus2, rect);//Drawing black for low voltage
                }
                ys.AccurateTimer.AccurateSleep(50);
                i++;
            } while (i<1000);
        }
    }
}


ys.cs:
C# codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ys
{
    public struct MSG    //structure MSG
    {
        public IntPtr handle;
        public uint msg;
        public IntPtr wParam;
        public IntPtr lParam;
        public uint time;
        public System.Drawing.Point p;

    }
    public class AccurateTimer
    {
        public static bool IsTimeBeginPeriod = false;

        const int PM_REMOVE = 0x0001;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool PeekMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin,
           uint wMsgFilterMax, uint wRemoveMsg);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool TranslateMessage(ref MSG lpMsg);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool DispatchMessage(ref MSG lpMsg);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool QueryPerformanceCounter(ref Int64 count);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool QueryPerformanceFrequency(ref Int64 frequency);

        public static int GetTimeTick()
        {
            return Environment.TickCount;
        }

        public static void AccurateSleep(float DlayTime)
        {
            Int64 Frequency = 0;
            Int64 StartTime = 0;
            Int64 EndTime = 0;
            float PassedMSec = 0;
            MSG msg;
            AccurateTimer.QueryPerformanceCounter(ref StartTime);   //get start time
            AccurateTimer.QueryPerformanceFrequency(ref Frequency); //get cpu's frequency
            do
            {
                if (AccurateTimer.PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE))
                {
                    AccurateTimer.TranslateMessage(ref msg);
                    AccurateTimer.DispatchMessage(ref msg);
                }
                AccurateTimer.QueryPerformanceCounter(ref EndTime);
                PassedMSec = ((float)(EndTime - StartTime) / (float)Frequency) * 1000;
            } while (PassedMSec <= DlayTime);  // delay  DlayTime
        }
    }

}


就这两个,你们先看看效果,应该和我的一样问题:就是黑白在闪的时候,有一条东西从上而下的划过,肉眼能看清楚,像是以前黑白电视那种东西...能解决这个问题的,我估计只有超级高手了。
 
 
搜索更多相关主题的帖子: private 
2011-06-10 16:43
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:10 
闪烁速度太快了,绘制一个矩形GDI从上到下绘制,黑框从上往下画,当还没画完,白框又从上往下画,就一半白一般黑,GDI慢?窗体绘制速度慢?

站在春哥的肩膀上
2011-06-11 09:23
vachul
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-10
收藏
得分:0 
以下是引用xydddaxia在2011-6-11 09:23:46的发言:

闪烁速度太快了,绘制一个矩形GDI从上到下绘制,黑框从上往下画,当还没画完,白框又从上往下画,就一半白一般黑,GDI慢?窗体绘制速度慢?
就算是先绘制完,存在虚拟画布上,然后再贴过去也是有这个问题存在
2011-06-13 13:15
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
存在虚拟画布上,显示的时候还不是要靠系统用gdi画上去

站在春哥的肩膀上
2011-06-14 09:40
vachul
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-10
收藏
得分:0 
回复 4楼 xydddaxia
那有什么办法没?
2011-06-14 10:00
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
适当降低降低刷新速率,
ys.AccurateTimer.AccurateSleep(50);停顿50毫秒,再延长一点,另外,在form的构造函数中启用双缓存绘图
public Form1()
        {
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();
        }
水纹现象能减少不少,一般显示器刷新60Hz,你这里代码相当于是20Hz刷新,人眼大概接收的连续图像20-24Hz
水纹现象从此而来?

站在春哥的肩膀上
2011-06-14 11:37
vachul
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-10
收藏
得分:0 
回复 6楼 xydddaxia
显示器60hz刷新,我的20hz刷新比它少应该不会有问题才对啊
2011-06-15 10:54
Jian_X
Rank: 4
等 级:业余侠客
威 望:1
帖 子:51
专家分:212
注 册:2009-10-23
收藏
得分:10 
回复 楼主 vachul
为什么不用Thread.Sleep(50),我用这个试没有任何问题
2011-06-15 17:02
快速回复:C# GDI+ 出现的令人诧异的问题,来看看,或许你也从未遇到过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067137 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved