Title: Flipping bitmap Function in C#
Tags: c#;graphics;bitmap;gdi+
Question: as a Practice I tried flipping a bitmap with unsafe code and pointers in C#
but the problem is I get the original bitmap as the result instead of flipped bitmap , it seems that the function does nothing at all , so I'm wondering what's wrong in my code !
please keep in mind that I want to flip pictureBox1.Image and set it as pictureBox2.Image
private bool Flip_H()
try
{
b = new Bitmap(pictureBox1.Image);
bmdata = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int offset = bmdata.Stride - b.Width * 3;
byte back_up;
int BGRwidth = b.Width * 3;
unsafe
{
byte* p = (byte*)bmdata.Scan0;
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < BGRwidth / 2; x += 3)
{
back_up = p[x];
p[x] = p[BGRwidth - x - 3];
p[BGRwidth - x - 1] = back_up;
back_up = p[x + 1];
p[x + 1] = p[BGRwidth - x - 2];
p[BGRwidth - x - 2] = back_up;
back_up = p[x + 2];
p[x + 2] = p[BGRwidth - x - 1];
}
p += offset;
}
}
b.UnlockBits(bmdata);
pictureBox2.Image = b;
return true;
}
catch
{
return false;
}
I have already done this with GetPixel() and SetPixel() functions , but as you know they are too slow , so I'm trying to Improve my code with pointers !
the Problem was Offset Parameter  ! thanks everyone who helped
Comment: Mind telling us what is wrong first? Please explain what _is_ happening that you are not expecting. Errors and exceptions too, if you are getting any.

Comment: no , the purpose is not flipping , I want to know pointers and unsafe codes in c# better , so I tried to flip a bitmap ! and I need Pointers for Image Processing in future :)

Comment: @Kenneth K , thank you too , the problem was exactly the offset !

Comment: Should this be on [codereview.stackexchange.com](http://codereview.stackexchange.com) then?

Comment: Your code actually is working...it's just working with the first scan line. If you look at the very top of `picturebox2`, you should see a reversed line (this might depend on whether or not `b.Height` is even or odd--I haven't checked).

Comment: What is wrong with `RotateFlip`? Is that too slow for you? Are you running this code repeatedly in a loop?

Here is the accepted answer: Your problem is that you never advanced in the Y direction. You always change the pixels only on the first scanline and therefore the rest does not change. This happens because you do p += offset but you defined offset as:
int offset = bmdata.Stride - b.Width * 3;
You should instead define offset as:
int offset = bmdata.Stride;
Since you want to add the stride width, in bytes, to get to the next scan line.
Also there is a bug in your swapping code. You have:
back_up = p[x + 0];
p[x + 0] = p[BGRwidth - x - 3];
p[BGRwidth - x - 1] = back_up;  // Error!
back_up = p[x + 1];
p[x + 1] = p[BGRwidth - x - 2];
p[BGRwidth - x - 2] = back_up;
back_up = p[x + 2];
p[x + 2] = p[BGRwidth - x - 1];
// Missing!
You should have:
back_up = p[x + 0];
p[x + 0] = p[BGRwidth - x - 3];
p[BGRwidth - x - 3] = back_up;
back_up = p[x + 1];
p[x + 1] = p[BGRwidth - x - 2];
p[BGRwidth - x - 2] = back_up;
back_up = p[x + 2];
p[x + 2] = p[BGRwidth - x - 1];
p[BGRwidth - x - 1] = back_up;
Comment for this answer: @Dotrix Yes it is, and your `offset` is wrong. By subtracting the width of the image times the number of bytes per pixel, you effectively reduced `offset` to 0 or near 0.
Comment for this answer: @Dotrix If your colors are off or strange, take a look at the PixelFormat you're using. You use 24-bit RGB but most images are 32-bit ARGB. You might consider using the pixel format of the source image instead, but this will make your code a bit more complicated.
Comment for this answer: what about " p += offset; " ? Isn't it changing the y direction ?
Comment for this answer: thanks , I changed it but now I get something else instead :D  , and also thanks for the other bug ! :)
Comment for this answer: @Virtlink Thanks body , The function is working fine now , I'll keep that in mind too , thanks again
Comment for this answer: I thought that at first, but I think that's what the purpose of `p += offset;` is. Perhaps he needs to check that `offset` is not zero.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:03/15/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c#><graphics><bitmap><gdi+>', 'Title': 'Flipping bitmap Function in C#', 'ViewCount': 859, 'id': 15432546, 'user_AboutMe': '<p><img src="http://i.stack.imgur.com/yEa2G.jpg" alt="Mehran Davoodi"></p>\n\n<ul>\n<li>C# programmer and SQL Server Designer</li>\n<li>2D and 3D Graphics and Animation Designer </li>\n</ul>\n\n<p>Fields of Interest : </p>\n\n<ul>\n<li>Web and Game Developing</li>\n<li>Image Processing</li>\n<li>2D and 3D Graphics and Animations</li>\n</ul>\n', 'user_AccountId': 2015992.0, 'user_DisplayName': 'Mehran', 'user_Reputation': 1401.0, 'user_Views': 236.0, 'user_id': 1803292.0}}