Hello,
How can i copy custom pixel wrom Bitmap to another Bitmap.
Example:
Source(0,0,640,480) -> Destination(10,10,40,40) where
params(x,y,width,height)
I tried with folowing code, but i got some strange results.
Code worked only wor examles where:
pt1.X = ....61,65,69,...
Please Help, and sorry for my English:)
//INPUT
// pt1 = top-left point ;pt2 = bottom-right point, bmpOrg = Source
Bitmp
public static Bitmap Copy(Point pt1,Point pt2,Bitmap bmpOrg)
{
int PixelSize = 3;
int width = pt2.X-pt1.X;
int height = pt2.Y-pt1.Y;
Bitmap bmpCpy = new Bitmap(width,height);
BitmapData bDataOrg = bmpOrg.LockBits(new
Rectangle(0,0,bmpOrg.Width,bmpOrg.Height),ImageLockMode.ReadWrite,bmpOrg.PixelFormat);
BitmapData bDataCpy = bmpCpy.LockBits(new
Rectangle(0,0,bmpCpy.Width,bmpCpy.Height),ImageLockMode.ReadWrite,bmpOrg.PixelFormat);
unsafe
{
byte *pCpy = (byte*)bDataCpy.Scan0;
byte *pOrg = (byte*)bDataOrg.Scan0;
int nOffset = bDataOrg.Stride - bmpOrg.Width*PixelSize;
int nWidth = bmpOrg.Width * PixelSize;
for (int i=0;i<bmpOrg.Height;i++)
{
for (int j=0;j<bmpOrg.Width;j++)
{
//Copy from desired area
if(j>=pt1.X && j<pt2.X && i>=pt1.Y &&
i<pt2.Y)
{
pCpy[0] = pOrg[0];
pCpy[1] = pOrg[1];
pCpy[2] = pOrg[2];
pCpy+=3;
}
pOrg+=3;;
}
pOrg+=nOffset;
}
}
bmpOrg.UnlockBits(bDataOrg);
bmpCpy.UnlockBits(bDataCpy);
return bmpCpy;
}
Bob Powell [MVP] - 29 May 2004 23:00 GMT
Is there some reason that you want to use LockBits? It's a great way to do
things if you need speed but not necessary for copying bitmap bits from
place to place.
You can draw on any bitmap by obtaining a Graphics obect for it. So, to copy
the bits in the area (0,0,640,480) into a second bitmap at (10,10,40,40) you
can do the following.
Bitmap bmA=(Bitmap)Image.FromFile("bitmapA.bmp");
Bitmap bmB=(Bitmap)Image.FromFile("bitmapB.bmp");
Graphics g=Graphics.FromImage(bmB);
g.DrawImage(bmA,new Rectangle(10,10,40,40),0,0,640,480,GraphicsUnit.Pixel);
Hope this helps.

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
> Hello,
>
[quoted text clipped - 25 lines]
>
> BitmapData bDataOrg = bmpOrg.LockBits(new
Rectangle(0,0,bmpOrg.Width,bmpOrg.Height),ImageLockMode.ReadWrite,bmpOrg.Pix
elFormat);
> BitmapData bDataCpy = bmpCpy.LockBits(new
Rectangle(0,0,bmpCpy.Width,bmpCpy.Height),ImageLockMode.ReadWrite,bmpOrg.Pix
elFormat);
> unsafe
> {
[quoted text clipped - 29 lines]
> return bmpCpy;
> }
Danijel Klemencic - 30 May 2004 00:37 GMT
Thanks for quick reply Bob.
Unfortunately i must use LockBits. So if anyone knows a fastest way,
please let me know.
My task is to select a region from image and copy this region into new
image. Like Photoshop.
Example:
Copy selected -> Paste as New Image.
Thanks in advance.
Bob Powell [MVP] - 30 May 2004 11:23 GMT
You'll forgive me for asking again but Why lockbits? I can understand if
you're aiming to do 150 paste operations per second but selecting an area
and copying can be accomplished just fine with the code I showed you and you
won't have to worry about the pixel format of the images you're pasting.
Because my telepathic interface is still in alpha testing at the moment
you'll need to explain in depth :-)

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
> Thanks for quick reply Bob.
>
[quoted text clipped - 8 lines]
>
> Thanks in advance.
Danijel Klemencic - 30 May 2004 13:35 GMT
I'll give it a try with code you wrote. Hope my profesor is going to be
setisfied:) He prefers manipulating with bits. (The application is
seminar work in Multimedia, so he prefers using bits and manipulating
with memory)
Thank you.
Bob Powell [MVP] - 30 May 2004 16:16 GMT
Ok, I see now.
The GDI+ FAQ has an article on using the LockBits method. This might help
you out.
The problem is that in your original question you specified that you wanted
to move an area from on bitmap to another but that the source and
destination wouldn't be the same size.
In a commercial programming situation you would obviously use the API calls
available to you if they exist. In a course however the intent is probably
to find out whether you understand the implications of doing such a task.
To move a large source area into a small destination you need to be thinking
about averaging a block of pixels together or some such method. You can
certainly read the source and write the destination with LockBits but it
won't do anything clever like stretching pixels for you.

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Image transition effects, automatic persistent configuration and
design time mouse operations all in April's issue of Well Formed
http://www.bobpowell.net/wellformed.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41
> I'll give it a try with code you wrote. Hope my profesor is going to be
> setisfied:) He prefers manipulating with bits. (The application is
> seminar work in Multimedia, so he prefers using bits and manipulating
> with memory)
>
> Thank you.