c# - Moving Bitmap region -


I'll give you a proxycode snippet to understand my question further:

  / / Dx (i.e. offset value) may be irregular (i = 0; i & lt; bitmap.columnsCount - dx; i ++) for {// "=" means a second column pixels. BitmapCallum [I] = BitmapKulum [I + DX]; }   

How should I do this? Of course I can take raw pixels through lockbitmap and then somehow use martial copy or insecure section ... but it's ugly and very complex. What better way?

I tried to find something similar to the MoveBitmapRegion () method, but I can not. The idea of ​​own drawing bitmap does not work:

  Graphics G = Graphics Framesize (bmp); G.DrawImage (BMP, 0, 0, new rectangle (DX, 0, BMP Wideth - DX, BMP. Highlight), Graphics Unit Pixel);   

It helps to copy the bitmap, but I think it is a very expensive operation:

  Graphics G = Graphics. Framesize (bmp); G.DrawImage (new bitmap (bmp), 0, 0, new rectangle (dx, 0, bmp.Wid - dx, bmp.height), graphics unit pixel);    

OK, just at the end of the day just this has been lifted very quickly There are some mistakes, but the image I examined was doing fine.

  Private static zero copyBmpRegion (bitmap image, rectangle, point deleted location) {// Some sanitizing logic (if (! ((SrcRect.X> = 0 & amp; srcRect .Y & gt; = 0) & amp; amp; ((srcRect.X + srcRect.Width) & lt; = image.Width & amp; amp; (SrcRect.Y + srcRect.Height) & lt; = image .ightight)) Exclude new logic exception ("source rectangle is not within the bounds of image."); If ((Location of location. X  image.Width) || (Dest., Y & L; 0) Dist location .Y> Image. Exclamation of logic ("The destination should be inside the image."); // bits to bits in BitMapData bmpData = image.LockBits (new rectangle (Point.Lite, Image Size), ImageLockMode.ReadWrite, image.PixelFormat); Int pxlSize = (bmpData.Stride / bmpData.Width); // Calculate pixel width of current image (in bytes) int src = 0; Int dest = 0; // Source / Destination Pixels // This is the account for the fact that all the source rectangles may be able to copy in destination: int width = (destLocation.X + srcRect.Width) & lt; = Image.Width? SrcRect.Width: (image.Width - (destLocation.X + srcRect.Width)); Int height = (destLocation.Y + srcRect.Height) & lt; = Image.High? SrcRect.Height: (image.Height - (destLocation.Y + srcRect.Height)); // Managed buffer byte [] buffer = new byte [pxlSize] to capture current pixel data; Calculate the beginning of the current source pixel and destination pixels for (int x = 0; x   

Basically you describe the source rectangle in the area that you want to copy (in pixels, not in bytes) i.e. rectangle (0, 0, 100 , 100) A block describes 100x100 pixels, starting from the top left corner of the expanded image.

After this, you describe the upper left corner of the destination rectangle. The source rectangular will be copied as much as possible, but if the image is not wide / width to accommodate the entire rectangle then it will be cut.

And the example of the experiment will be the following:

  image img = Image.FromFile (@ "C: \ user \ 254288b \ downloads \ mozodojo-original-image.jpg" ); CopyBmpRegion (bitmap) IMG, new rectangle (5, 5, 100, 100), new point (100, 100)); Img.Save (@ "C: \ Users \ 254288b \ Downloads \ mozodojo-new-image.jpg", ImageFormat.Jpeg);   

What were the following results:

See how it goes.

Comments