Click here to Skip to main content
15,563,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello , My project is to make my own jpg format .

I have a 256/256 px bmp and what I want to do is this : when I click inside the picture I want to take the coresponding square of 8/8 px and display it in another pictureBox .I need to do this because I want to dispalay the smaller picture and the corespoding matrice in the same time.

I`am sorry for my english , If you don`t understand want I mean ask me and I will try to explain this situation better.
Sergey Alexandrovich Kryukov 6-Dec-11 13:04pm    
Forms? Tag it? It's not 100% apparent what UI and imagine library do you want to use.
The problem is pretty simple, so what seems to be difficult for you.

You don't need to create a new image format. You can crop the existing image and display it in the second picturebox

Bitmap img = new Bitmap(ExistingImage);
Bitmap cropped = img.Clone(new Rectangle(0, 0, 8, 8), bmpImage.PixelFormat);
pictureBox2.Image = cropped;
Share this answer
Radu_Socaciu 6-Dec-11 14:08pm    
This solution works for me . it is grate , but i still have a problem.

<pre lang="c#">
Bitmap img = new Bitmap(imgIncarcata.Image);

Bitmap cropped = img.Clone(new Rectangle(coorX, coorY, 80, 80), imgIncarcata.Image.PixelFormat);

pbBlocOrig.Image = cropped;
catch (OutOfMemoryException oome)
Ok. Now when I click in some areas of the picture it works perfectly(top left corner) , but when I try to click on the right down corner it throws an exception :
System.OutOfMemoryException: Out of memory.
at System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format)
at JpgProject.lblE.imgIncarcata_MouseClick(Object sender, MouseEventArgs e)
[no name] 6-Dec-11 14:21pm    
If you click the lower right corner, at coordinates 256,256 then of course you will get this exception. There is nothing to clone, the image does not extend extend beyond. You will need to implement checks to ensure the bounding rectangle is valid.
Radu_Socaciu 6-Dec-11 14:28pm    
I am so stupid . Thank you very much for your replay . The exception was odd this is why I was confused .
One more time Thank you very much.
Here is the key: to access bitmap pixels, you need to use System.Drawing.Bitmap.LockBits. for both source bitmap and a new small bitmap you want to show. It's important not to use GetPixel/SetPixel, due to performance problem.

Please see:[^].
See the code sample here:[^].

Handle the event MouseDown on your picture box; event arguments parameter passed to your handler will give you mouse coordinates which you will translate into bit address.

Share this answer
Radu_Socaciu 6-Dec-11 13:23pm    
Thank you for your replay .
My goal is to edit the onClick event . My source img is a 256/256 bmp and what I want to do is : when I click with the mouse on a random point: for exp x=30, y=67 I want to be able to take all that 8 by 8 square of pixels where this point is placed.
Sergey Alexandrovich Kryukov 6-Dec-11 16:13pm    
I already answered what basically to do. OK, you can handle Click event (there is not event onClick or OnClick, but you can create a control (derive if from Control as you don't really need PictureBox) and override OnClick instead). So, do you have a problem implementing it? If you do, you have to be more specific.
Radu_Socaciu 7-Dec-11 8:05am    
Thank you for your replay . I`ve managed to solve my problem with Mark Nischalkes solution . Your links were very helpfull and I will use that information for the following steps of my project.
Next I want to zoom the selected square 10 times and store it in another pictureBox.

One more time Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900