![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
private void pictureBox1_Resize(object sender, EventArgs e) { int w = pictureBox1.Width; int h = pictureBox1.Height; pictureBox1.BackgroundImage = new Bitmap(w, h); pictureBox1.Image = new Bitmap(w, h); Display(); } In this piece of code a Resize event is triggered again by the line pictureBox1.Image = new Bitmap(w, h); Why is this happening, and why does the picture box revert to its previous size when this occurs. I have the PictureBox Dock property set to Fill. The PictureBox is on a Panel which also has its Dock property set to Fill. I set a breakpoint at the int w line, then hit the maximize button. The program stops at the break point and I single step. w and h get assigned values appropriate for my full screen window. When continuing to single step the assignment to the Image property causes the Resize event to occur again and single step jumps back to the int w line. The pictureBox1 Width and Height have reverted back to what they were before the maximize button was clicked. When running at full speed (no breakpoint or stepping), the Window is maximized but the bitmap of the pre maximize size is displayed in the top corner of the PictureBox area. Adding the line: Debug.WriteLine(w.ToString() + " " + h.ToString()); following the assignment to w and h give the following: 1280 822 931 383 Here is my Display function, which sets up for drawing on the bitmap. private void Display() { // reflect about the x axis, to have positive y point up // and translate y = 0 to bottom of the picture box Matrix myMatrix = new Matrix(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, pictureBox1.Height); Graphics g = Graphics.FromImage(pictureBox1.BackgroundImage); g.Transform = myMatrix; // translate y = 0 to the bottom of the picture box graph.Display(new Canvas(g, new Size(pictureBox1.Width, pictureBox1.Height), this.Font)); } The reason for using the bitmaps in this manner instead of just drawing on the PictureBox during a paint event, is so that I can have the 2 layers to draw on. The BackgroundImage display the graph and the Image displays the rubber band rectangle for mouse selection. Bill |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Thanks Bob for setting me on the right track. The following code works, but if I assign the Bitmap to the Image instead of the BackgroundImage the rest of the form doesn't display properly until after I move it with the mouse. private void pictureBox1_Paint(object sender, PaintEventArgs e) { if ((itsBitmap.Width != pictureBox1.Width) || (itsBitmap.Height != pictureBox1.Height)) { itsBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); GenerateBitmap(); } pictureBox1.BackgroundImage = itsBitmap; } private void GenerateBitmap() { // reflect about the x axis, to have positive y point up // and translate y = 0 to bottom of the bitmap Matrix myMatrix = new Matrix(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, itsBitmap.Height); Graphics g = Graphics.FromImage(itsBitmap); g.Transform = myMatrix; graph.Display(new Canvas(g, new Size(itsBitmap.Width, itsBitmap.Height), this.Font)); } Bill |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
If I can't change the PictureBox.Image in the Paint or Resize events how do I know when to update it for window resizing? Bill |
#7
| |||
| |||
|
|
Why do you think you can't change it in the Resize event? That's what it's for... |
|
The picturebox will resize because of the size mode and the fact that an image created in memory may not be the real resolution of your screen. |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
Bob Powell [MVP] wrote: Why do you think you can't change it in the Resize event? That's what it's for... I was going by your original response to my question. The picturebox will resize because of the size mode and the fact that an image created in memory may not be the real resolution of your screen. From this it looked like you were telling me that my second resize event was being caused by the assignment to Image in the Resize event handler. Therefore I assumed that assigning to Image inside the Resize event was not a good idea. What I will do now is put something simple in my Resize event like you have in your example and see what happens. If all goes well with this, I will start looking for other things in my code which may be changing the size of the PictureBox. Thanks for the advice Bob. I am only taking this thread as far as anyone wants to go with it. When you have better things to do than give me free advice, feel free to start ignoring me. This is for a work project, but my intention is to post the code on my website. I also plan to fix the resize problem in my Fractal program which I have posted at: http://www.componentsnotebook.com/no.../fractals.aspx I had this problem with resize in that program years ago. Since it wasn't critical I never got around to solving it. In that case the Image needs regenerated anyway, and it takes a long time, I just let the user click the Draw button to change the size of the BackgroundImage. thanks Bill Bill |
#10
| |||
| |||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |