• Advertisement
Sign in to follow this  

[.net] flicker on redrawing the controls

This topic is 3317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi ! I am implementing Zoon functionality in .netCF using C#.For that I am redrawing the picture box .there are some graphics also on the picture box. When I click on button for Zooming In or Zooming Out, the control redraws and it flickers.How it can be removed?I cant use setstyle or any double buffering methods as they are not allowe din .net Cf Please help Roohi

Share this post


Link to post
Share on other sites
Advertisement
Try this

this code works like a charm, really. Without flickering, side effects or anything strange:)



using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace DoubleBufferingPictureBox
{
public partial class Form1 : Form
{
private float m_zoomFactor;
private Bitmap m_origBitmap;

public Form1()
{
InitializeComponent();
m_zoomFactor = 1;
}

private void Form1_Load(object sender, EventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
path = path + @"\TestInt.Bmp";
m_origBitmap = new Bitmap(path);
drawPictureBox(pictureBox1.CreateGraphics(), true);
}

private void drawPictureBox(Graphics graphics, bool ownGraphics)
{

using (Bitmap tempBmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
using (Graphics tempG = Graphics.FromImage(tempBmp))
{
tempG.Clear(Color.White);

//Now zoom from only 0,0 point
tempG.DrawImage(m_origBitmap, new Rectangle(0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1),
0, 0, (int)(m_origBitmap.Width / m_zoomFactor),(int)(m_origBitmap.Height / m_zoomFactor),
GraphicsUnit.Pixel, new System.Drawing.Imaging.ImageAttributes());



Rectangle srcRect = new Rectangle(0, 0, tempBmp.Width, tempBmp.Height);
graphics.DrawImage(tempBmp, 0, 0, srcRect, GraphicsUnit.Pixel);
}


}

if (ownGraphics)
{
graphics.Dispose();
}
}


private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
drawPictureBox(e.Graphics, false);
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
m_zoomFactor += 0.1f;
if (m_zoomFactor >= 3f)
{
m_zoomFactor = 1f;
}

drawPictureBox(pictureBox1.CreateGraphics(), true);

}
}
}

or
I read this article http://www.bobpowell.net/pictureboxhowto.htm

which says picturebox.creategraphics is not the right way of using the picturebox.

pictureBox1.Image = Properties.Resources.Water_lilies;

using (Graphics g = Graphics.FromImage(pictureBox1.Image))

now use this graphics object g for drawing image or drawing other things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement