473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# PictureBox Help

I have a PictureBox that draws an image and then a rectangle. However, annoyingly, any other window that I put over the rectangle causes it not to redraw. (I can cut out half of the rectangle by opening Notepad and placing it over half of it, or all of it by minimizing and then maximizing)

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.         private void Refresh_PictureBox()
  2.         {
  3.             Graphics g = pictureBox1.CreateGraphics();
  4.  
  5.             g.DrawImage(pictureBox1.Image,
  6.             new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
  7.             pictureBox1.Bottom - hScrollBar1.Height),
  8.             new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
  9.             pictureBox1.Right - vScrollBar1.Width,
  10.             pictureBox1.Bottom - hScrollBar1.Height),
  11.             GraphicsUnit.Pixel);
  12.  
  13.             foreach (NPC npc in NPCs)
  14.             {
  15.                 if (npc.ID == NPCSelectedID)
  16.                 {
  17.                     Point p1 = new Point(npc.startx - hScrollBar1.Value, npc.starty - vScrollBar1.Value);
  18.                     g.DrawRectangle(new Pen(Color.Red), new Rectangle(p1, new Size(npc.width, npc.height)));
  19.                 }
  20.             }
  21.  
  22.             pictureBox1.Update();
  23.  
  24.             g.Dispose();
  25.         }
Don't worry about the values of the coordinates or the NPC stuff. There must be something wrong with my ordering or my structuring of creating graphics specifically.

This function gets called on HandleScroll and Click for the picturebox, as well as on the form Resize. And, certainly enough, the rectangle comes back when any of these events are done. But why would the rectangle disappear and not the rest of the image? That's what bothers me.

If you would like to see more code just say so.

Thanks,
Scott
Jul 30 '08 #1
4 1729
Plater
7,872 Expert 4TB
Attach a handler to the Paint event and put your code in there.
Jul 30 '08 #2
Thanks for the advice.

It's weird now. Now it does redraw, but only when I move the Notepad window slowly enough. If I move it fast, it never comes back.
Jul 30 '08 #3
Changing the actual pixels of the picturebox's image is very rigid, but also very sluggish. I'd rather use the Draw methods of the Graphics class, but I'm still having problems with redrawing. Any ideas to get it close to as rigid as changing the actual pixels?

Any more help would be appreciated,
Scott
Jul 30 '08 #4
I just went down to the byte level of the image instead of the sluggish Get and SetPixel(), and now it works much faster. If anyone wants help with how I did this, feel free to ask.
Jul 31 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Pavan Arise | last post by:
Dear all.. I have a picturebox filled on a form. The picturebox has some graphics displayed on it.I was trying to save the picturebox, but continuesly failed to do so. I am clueless of why it is...
0
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
4
by: munglet | last post by:
Is there anyway to get a picturebox to recieve focus? I ask because I implemented a "delete" button in a picturebox (due to size constraints), but now due to accessibility reasons I need to be...
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
4
by: munibe | last post by:
Hi, i have a problem about picturebox control. if you may help me, i will be so happy. i have a picturebox named pic_map, and i added a button named customer_button, my wish is to add a new small...
4
by: Jim McGivney | last post by:
In C# on Form1 I genetate an array of PictureBoxes and populate each with an image as seen in the code below. Later on I want to access a specific PictureBox to change its image, but I keep...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
5
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the...
1
by: leshka82 | last post by:
I have recently designed a Winform Image drag & drop utility. The approach I took was to have a Panel control serve as a destination for multiple file drop. Once the user dragged & dropped the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.