In C#.NET or WPF C#, I want to copy an image (say image2) and paste on an already existing image (say image1) and then be able to move image2 on image1 to any other location within image1 and also be able to delete image2.
Asked
Active
Viewed 264 times
-3
-
Possible duplicate of [C# copy paste an image region into another image](https://stackoverflow.com/questions/9616617/c-sharp-copy-paste-an-image-region-into-another-image) – Martheen Oct 26 '19 at 12:56
-
No Martheen. I am new to C# image based programming and did not find suitable code to go through and understand the process of moving or deleting the image that gets copied on to another image. Though, copying is possible but just that. – oparthan Oct 26 '19 at 13:31
-
You can combine setting an PictureBox.Image=image1 and the Paint event e.Graphics.DrawImage(image2, x, y). Change x & y and trgger Paint by pbox.Invalidate. When done export by either the link to copy or by pbox.DrawToBitmap. – TaW Oct 26 '19 at 13:35
-
1You can't actually "move" and "delete" an image from another image unless you're working with layers. So what you actually want is simply keep the original image1, and when you need to "move", just re-copy the image2 to a different position, and when you need to "delete", just re-use the original image you kept. – Martheen Oct 26 '19 at 13:56
-
Thank you, TaW; I shall try your advice. Martheen, your logic is fine if only one image need be added and deleted. What if I want to add more than 1 (say n no. of images) and then select a particular image to delete that one or move a particular image to a new position? And how to do layering; that seems a good advice. – oparthan Oct 26 '19 at 14:12
-
If you have multiple images, then just create a list containing the image and where do you want to put each of them, then create a blank image and loop through the list every time you need to render. That way deleting or moving is simply editing the list. That's also how layering works (sorta, additional logic such as transparency can simply use https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.compositingmode but otherwise you'll need to loop through every pixel) – Martheen Oct 27 '19 at 03:40
-
Thank you Martheen. My issue with moving and deleting is when at a later time (say, after few days of saving image1, image2, image3 on imageMain, i may want to move image 2 to the position occupied by image1 and before doing so, i may want to delete image1) i want to effect some changes, i should be able to do so. Your logic, i feel, is to do all at once at a given time. – oparthan Oct 27 '19 at 04:25
-
And how would you propose "moving" at later time *without* keeping the original images? Think about it. It's like overwriting a paragraph in regular text file (not Word or Google Doc) then wanting to somehow recover the overwritten text. You can't, because if you can do it, I can just "copy" a thousand images into a single image, send/store it, then later somehow "delete" the layers, creating a thousand images from one, without losing information, that's infinite compression. So how did Word/Google Docs change tracking works, or Photoshop layer works? By keeping the original text/images. – Martheen Oct 28 '19 at 07:23
-
Yes Martheen. I keep the original image as it is. And for later retrieval of placed images, I would store their respective locations by their X,Y references in database or css file. So, whenever I retrieve the main image with other images placed on it, the program should read the database/css and place the images at their respective positions. And when I want to effect some changes like move one image to a newer position and then save, the newer coordinates get overwritten on the previous saved coordinates data. Isn't it so!?! And my apologies for late reply. – oparthan Oct 31 '19 at 08:16
-
Hi Martheen, I could get my objective solved, by combining lambda expressions (may be we don't require lambda expressions, but I shall test the same later) with drag drop events. All my requirements as I had asked above have been achieved, thanks to Google, Youtube videos and your thought provoking answers. – oparthan Nov 10 '19 at 14:46
1 Answers
0
I could get my objective solved, by combining lambda expressions (may be we don't require lambda expressions, but I shall test the same later) with drag drop events. All my requirements as I had asked above have been achieved, thanks to Google, Youtube videos and Martheen's thought provoking contributions. Lambda expressions for mouse down and mouse move, after normal dragdrop event handling. Virtual pictureboxes created within the lambda expressions, from picturebox2 or the foreground image to be moved around. Therefore, many images of same picture (picturebox2) could be created, each image was easily movable and each image could be deleted without affecting background image (or picturebox1).

oparthan
- 1