Graphics.DrawImage problem

J

Jeroen Ceuppens

Hi,

When i do this in my programma, in the contstructor of the form:
g.DrawImage(i,new
Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel);

it doens't paint it ;( but if I make a button with the same, after the click
it paints it, why the problem, i can't get out of it.........



Here you see all the code:

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Windows.Forms;

using System.IO;

namespace RubberBand1

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.ComponentModel.IContainer components;

//private Bitmap bitmap, bm;

//private bool MenBarInit = false;

private bool drag = false;

private int DrawMode = 1;

private int x0, y0, x, y;

private int cx, cy;

//private Graphics gB;

private Graphics g;

private Image i;

SolidBrush b=new SolidBrush(Color.Blue);

private System.Windows.Forms.Button button1;

Point[] pp=new Point[3];



public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();


//

// TODO: Add any constructor code after InitializeComponent call

//

//SetStyle(ControlStyles.UserPaint, true);

//SetStyle(ControlStyles.AllPaintingInWmPaint, true);

//SetStyle(ControlStyles.DoubleBuffer, true);

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(8, 184);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(152, 56);

this.button1.TabIndex = 0;

this.button1.Text = "LAADIN";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(1000, 700);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

this.Load += new System.EventHandler(this.Form1_Load);

this.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);

this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

i=Image.FromFile(@"d:\ImageWeftIllum.bmp");

g=this.CreateGraphics();

//g.DrawImage(i,new
Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel);

//bitmap= new Bitmap(@"d:\ImageWeftIllum.bmp");

//bitmap= new Bitmap(640,480);

//RefreshBackground(); // om een of andere reden wilt hem dan pas tekenen,
en niet al bij formload


}

private void Form1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)

{

Point p = new Point(e.X, e.Y);

x0 = p.X;

y0 = p.Y;

drag = true;

}

private void Form1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)

{

Point p = new Point(e.X, e.Y);

x= p.X;

y = p.Y;

cx = x - x0;

cy = y - y0;

if (drag)

{

Invalidate();// //causes a paint message to be sent to the control

//RefreshBackground(); toont basis image nog, maar vergeet track te tekenen

//Invalidate(); toont basis image niet, maar tekent track

}

}

private void Form1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)

{

cx = x - x0;

cy = y - y0;

Pen pen = new Pen(Color.Blue);

switch (DrawMode)

{

case 1:

{

g.DrawRectangle(pen, x0, y0, cx, cy);

pp[0].X=700;

pp[0].Y=20;

pp[1].X=700+cx;

pp[1].Y=20;

pp[2].X=700;

pp[2].Y=20+cy;

g.DrawImage(i, pp, new Rectangle(x0,y0,cx,cy), GraphicsUnit.Pixel);

break;

}

}

//RefreshBackground();

drag = false;

pen.Dispose();

}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)

{

Graphics g = e.Graphics;

Pen pen = new Pen(Color.Blue);

if (drag)

{

switch (DrawMode)

{

case 1:

{

g.DrawRectangle(pen, x0, y0, cx, cy);

break;

}


}

}

pen.Dispose();

}




private void Form1_SizeChanged(object sender, System.EventArgs e)

{

RefreshBackground();

}

private void RefreshBackground()

{

//Size sz = this.Size;

//Rectangle rt = new Rectangle(0, 0, 640, 480);

//bm = bitmap.Clone(rt, bitmap.PixelFormat);// ook gebruiken bij eventueel
gebruik van deel kopieren


g.DrawImage(i,new
Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel);


//BackgroundImage = bm;

}

private void button1_Click(object sender, System.EventArgs e)

{

RefreshBackground();

}

}

}
 
M

Morten Wennevik

I think Paint is called after Page_Load and then everything is painted over
with the default background.

When you click the button you draw the image again, but you also should
include refreshbackground() in Form1_Paint
 
J

Jeroen Ceuppens

Thx for help, now it almost works, but the image flickers during the use of
the trackerselection ;-( How can i fix the flickering?

Greetz
Jeroen
 
M

Morten Wennevik

That I don't know. I have never used a trackerselection, but to minimize
flicker the trick is to draw as little as possible and only when necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top