Click here to Skip to main content
15,792,842 members
Home / Discussions / C#
   

C#

 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
SimpleData26-Feb-10 5:54
SimpleData26-Feb-10 5:54 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
Dan Mos26-Feb-10 6:02
Dan Mos26-Feb-10 6:02 
GeneralRe: Drawing onto a Windows Form without freezing it [modified] Pin
Luc Pattyn26-Feb-10 7:44
sitebuilderLuc Pattyn26-Feb-10 7:44 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
Saksida Bojan26-Feb-10 5:43
Saksida Bojan26-Feb-10 5:43 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
SimpleData26-Feb-10 6:17
SimpleData26-Feb-10 6:17 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
Saksida Bojan26-Feb-10 6:28
Saksida Bojan26-Feb-10 6:28 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
SimpleData26-Feb-10 6:44
SimpleData26-Feb-10 6:44 
GeneralRe: Drawing onto a Windows Form without freezing it Pin
Saksida Bojan26-Feb-10 8:00
Saksida Bojan26-Feb-10 8:00 
I have done it:

private Bitmap bit;
private Graphics dc;
// Used to suspend layout
private delegate void Suspend(bool b);
private Suspend mySuspend;

private void SuspendLayoutDel(bool b)
{
        if (b)
                this.SuspendLayout();
        else
        {
                this.ResumeLayout();
                // Set to true to force full redraw
                Invalidate(true);
        }
}

private void SetPixel(Point xy, Color clr, Graphics dc)
{
        Pen _pen = new Pen(clr, 1);

        //Lock prevents accsess from other threads
        lock(dc)
        {
                dc.DrawRectangle(_pen, new Rectangle(xy, new Size(1, 1)));
        }

        _pen.Dispose();
}

protected override void OnPaint(PaintEventArgs e)
{
        base.OnPaint(e);
        if (bit == null)
        {
                bit = new Bitmap(300, 300);
                dc = Graphics.FromImage(bit);
        }
                e.Graphics.DrawImage(bit,0,0);
}

private void button1_Click(object sender, EventArgs e)
{
        // Asign delegate so that Invoke is useed (Threat Safety)
        mySuspend = new Suspend(SuspendLayoutDel);
        Thread th = new Thread(new ThreadStart(Dummy));
        th.Name = "Drawer";
        th.Start();
}

private void Dummy()
{
        this.Invoke(mySuspend, new Object[]{true});
        Random rnd = new Random();
        int buff = 0;

        for (int y = 0; y < 300; y++)
        {
                for (int x = 0; x < 300; x++)
                {
                        buff = rnd.Next(0, 4);
                        if (buff == 0)
                                SetPixel(new Point(x, y), Color.Cyan,dc);
                        else if (buff == 1)
                                SetPixel(new Point(x, y), Color.Magenta, dc);
                        else if (buff == 2)
                                SetPixel(new Point(x, y), Color.Yellow, dc);
                        else if (buff == 3)
                                SetPixel(new Point(x, y), Color.Black, dc);
                }
        }
        this.Invoke(mySuspend, new Object[] { false });
}


I used graphic to draw to bitmap. Aka to memory. After all is displayed, it draws on screen. This aproach is olmost instantly compared to draw directly to screen.
GeneralRe: Drawing onto a Windows Form without freezing it Pin
Luc Pattyn26-Feb-10 7:46
sitebuilderLuc Pattyn26-Feb-10 7:46 
QuestionQuad Tree With images Pin
mallikharjunrao8426-Feb-10 4:17
mallikharjunrao8426-Feb-10 4:17 
AnswerRe: Quad Tree With images PinPopular
RugbyLeague26-Feb-10 4:29
RugbyLeague26-Feb-10 4:29 
GeneralRe: Quad Tree With images Pin
Pete O'Hanlon26-Feb-10 6:15
subeditorPete O'Hanlon26-Feb-10 6:15 
GeneralRe: Quad Tree With images Pin
Keith Barrow26-Feb-10 6:27
professionalKeith Barrow26-Feb-10 6:27 
QuestionListView Column Sort Arrow Pin
#realJSOP26-Feb-10 4:17
mve#realJSOP26-Feb-10 4:17 
AnswerTry this Pin
Pete O'Hanlon26-Feb-10 6:39
subeditorPete O'Hanlon26-Feb-10 6:39 
GeneralRe: Try this Pin
#realJSOP26-Feb-10 10:02
mve#realJSOP26-Feb-10 10:02 
GeneralRe: Try this Pin
Pete O'Hanlon26-Feb-10 10:46
subeditorPete O'Hanlon26-Feb-10 10:46 
GeneralRe: Try this Pin
#realJSOP27-Feb-10 1:55
mve#realJSOP27-Feb-10 1:55 
GeneralRe: Try this Pin
#realJSOP27-Feb-10 4:58
mve#realJSOP27-Feb-10 4:58 
GeneralRe: Try this Pin
Pete O'Hanlon1-Mar-10 2:12
subeditorPete O'Hanlon1-Mar-10 2:12 
GeneralRe: Try this Pin
Pete O'Hanlon1-Mar-10 10:34
subeditorPete O'Hanlon1-Mar-10 10:34 
AnswerRe: ListView Column Sort Arrow Pin
-tusk-22-Jun-11 7:10
-tusk-22-Jun-11 7:10 
GeneralRe: ListView Column Sort Arrow Pin
-tusk-24-Jun-11 3:57
-tusk-24-Jun-11 3:57 
Question2d array in c# Pin
Aljaz11126-Feb-10 4:05
Aljaz11126-Feb-10 4:05 
AnswerRe: 2d array in c# Pin
Pete O'Hanlon26-Feb-10 4:17
subeditorPete O'Hanlon26-Feb-10 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.