473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - APP: Image Processing - Copying channels between two bitmaps

9 New Member
I'm trying to use unsafe mode to do some operations on two different bitmaps.

-I loaded 2 images
-Called my Change method.
- Locked their Bitmap data as bmDataLeft and bmDataRight.
- Doing some operations on BGR channel bits.

The problem is that no operation between them is affected.
First look at my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         public static Bitmap Change(Bitmap LeftBitamp, Bitmap RightBitamp)
  3.         {
  4.             Bitmap LeftB = LeftBitamp;
  5.             Bitmap RightB = RightBitamp;
  6.             // the return format is BGR.
  7.             // This is where I lock the data so I can declare a pointer to them.
  8.             // bmDataLeft for the LeftBitmap, bmDataRight for the RightBitmap, 
  9.             BitmapData bmDataLeft = LeftB.LockBits(new Rectangle(0, 0, LeftB.Width, LeftB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  10.             BitmapData bmDataRight = RightB.LockBits(new Rectangle(0, 0, RightB.Width, RightB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  11.  
  12.             // The normal procedure for pointer to an image variables
  13.             int strideL = bmDataLeft.Stride;
  14.             IntPtr Scan0L = bmDataLeft.Scan0;
  15.  
  16.             int strideR = bmDataRight.Stride;
  17.             IntPtr Scan0R = bmDataRight.Scan0;
  18.  
  19.             unsafe
  20.             {
  21.                 #region 3 bytes
  22.                 // These are my actual data I will do operations on.
  23.                 byte* pL = (byte*) (void*) Scan0L;
  24.                 byte* pR = (byte*) (void*) Scan0R;
  25.                 // These are for the correct indexing.
  26.                 // There shouldn't be a problem here.
  27.                 int nOffsetLeft = strideL - LeftB.Width*3;
  28.                 int nOffsetRight = strideR - RightB.Width*3;
  29.                 int nWidthLeft = LeftB.Width*3;
  30.                 int nWidthRight = RightB.Width*3;
  31.  
  32.                 // Looping through all pixels.
  33.                 for (int y = 0; y < LeftB.Height; ++y)
  34.                 {
  35.                     for (int x = 0; x < nWidthLeft; ++x)
  36.                     {
  37.                         // This is the line that is not functioning properly
  38.                         // Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
  39.                         pL[0] = (byte)(pR[0]);
  40.  
  41.                         // But strangely this one does work! :
  42.                         // pL[0] = (byte) (pL[0]+50);
  43.                         // Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
  44.  
  45.  
  46.                         ++pL;
  47.                         ++pR;
  48.                     }
  49.                     pL += nOffsetLeft;
  50.                     pR += nOffsetRight;
  51.                 }
  52.  
  53.                 #endregion
  54.  
  55.  
  56.             }
  57.  
  58.             LeftB.UnlockBits(bmDataLeft);
  59.             RightB.UnlockBits(bmDataRight);
  60.             LeftB.Save("LeftOne.jpg");
  61.             RightB.Save("RightOne.jpg");
  62.  
  63.             return LeftB;
  64.         }
  65.  
  66.  
As you can see at the commented lines, When I try to use a data from a bitmap data on itself it works OK, but when I try to use both BitmapData datas I get no result!

I will appreciate all your solutions or if someone could tell me my mistake. Or may be in one Statement line there is only one bitmapData accesible in memory?

Thank you,

BestRegards,
Aram Azhari
Nov 27 '07 #1
6 2138
AramAz
9 New Member
Bump.
Anyone please ?
Nov 28 '07 #2
AramAz
9 New Member
Guys I found my problem. I was traversing through the data byte by byte. What I neeeded to do was to traverse pixel by pixel, meaning that I have increment the pointers by 3 and Change the loop to a one-third (before it was LeftB.Width*3 , now it is just LeftB.Width).

Expand|Select|Wrap|Line Numbers
  1.  
  2.                 for (int y = 0; y < LeftB.Height; ++y)
  3.                 {
  4.                     for (int x = 0; x < LeftB.Width; ++x)
  5.                     {
  6.                         // This is the line that is not functioning properly
  7.                         // Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
  8.                         pL[0] = (byte)(pR[0]);
  9.  
  10.                         // But strangely this one does work! :
  11.                         // pL[0] = (byte) (pL[0]+50);
  12.                         // Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
  13.  
  14.  
  15.                         pL+=3;
  16.                         pR+=3;
  17.                     }
  18.                     pL += nOffsetLeft;
  19.                     pR += nOffsetRight;
  20.                 }
  21.  
  22.         }
  23.  
  24.  

I hope this helps somebody else as well.

Regards,
Aram Azhari
Nov 28 '07 #3
Plater
7,872 Recognized Expert Expert
Was this something you could not do with the Bitmap class and it's GetPixel()/SetPixel() functions?
Nov 28 '07 #4
AramAz
9 New Member
Was this something you could not do with the Bitmap class and it's GetPixel()/SetPixel() functions?
I could, But that would take forever to complete the operation.
C# internal methods of GetPixel() SetPixel() are too slow to be used for image processing. It is more used for getting color of a point or painting which uses a very limited number of pixels.
What we're doing here is a lot of points and a lot of operations on them. For ex, if I have 2 pictures each size of 1024x768, and if I want to swap their blue channel it would take 1024*768*3 operations to do such a job. That is 2,359,296 operations.
GetPixel() SetPixel() is a slow process and it would takes even 5 minutes! This even gets worse when you want to do effects using convolution Matrixes (operation between 9 pixels)
but using pointers (pointing to direct memory addresses which hold the bitmap pixel datas) it may be up to 3 seconds!

I hope my explanation was helpful to you.

Regards,
Aram Azhari
Nov 28 '07 #5
Plater
7,872 Recognized Expert Expert
Could you use a MemoryStream object populated from the Raw Bitmap data and manipulate the bytes through there?
It looked like you were doing the stride and number of bytes per pixel calculations already so you would know which bytes to effect?

Although if you found the answer already it doesn't much matter I suppose. Plus, I've never been very good with API calls.
Nov 28 '07 #6
AramAz
9 New Member
Could you use a MemoryStream object populated from the Raw Bitmap data and manipulate the bytes through there?
It looked like you were doing the stride and number of bytes per pixel calculations already so you would know which bytes to effect?

Although if you found the answer already it doesn't much matter I suppose. Plus, I've never been very good with API calls.
Thank you for your reply Plater,

Using a MemoryStream is as fast as using pointers, and it has an advantage:
- It is NOT unsafe code!

As you said, I already found my mistake but I will try to do it in the Memory Stream way and post a similar solution using such.

Regards,
Aram Azhari
Nov 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3536
by: Michele Petrazzo | last post by:
I want to create a simple image editor (with wxPython) for my application. Now I'm able to zoom, draw with a pen, insert a text into the loaded image, and undo changes (for this I reload the image and redraw all the lines/text, but not the last.) My problem is how can I move the texts/lines that are insert into Image, after placed? How the...
8
2602
by: Jef Driesen | last post by:
I'm implementing some image processing algorithms in C++. I created a class called 'image' (see declaration below), that will take care of the memory allocations and some basic (mathematical) stuff. The class will behave like a std::vector (copy constructor and assignment create a deep copy), but with 2D indexing. Now I also need a 'view'...
4
8784
by: Kate Luu | last post by:
My application was written by C# and I couldn't load the photoshop image. Are there any Pro out there know how to load the photoshop image with the file extension is: .PSD. Thanks you all in advance, I'm greatly appreciate for your help. Have a good one... Kate Luu
4
3084
by: Christina Androne | last post by:
Hi I want to allow the user to drow some locators on an image and connect the locators with lines. So I tried to put the following code in the onmousedown enevt of the picture: Graphics LGraphics = Graphics.FromImage(imgMap.Image); LGraphics.DrawString("X", new Font("Arial", 12), SystemBrushes.WindowText, AX, AY);
3
1951
by: michael | last post by:
Hi, I am trying to write an ASP.NET web app, in Visual Basic.NET, using Visual Studio.NET 2004, .NET framework 1.1.4322 SP1 Running the project/app on localhost while in dev/write/debug stage When I say "trying", I do have it written, and it works ... sort of, for some cases. The problems/issues?
9
5517
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame. But the "app.MainLoop()" in wxpython looks like conflicting with the "while 1:" in socket server. After I commented the "app.MainLoop()",...
4
1641
by: Jon Slaughter | last post by:
Whats is there any reason to use a Bitmap(Image) over an Image(Bitmap)? I'm making a container to hold bitmaps sorta like the ImageList class(that doesn't fix the sizes) but I'm not sure if I should use Images or Bitmaps? I don't know of the real distinction between them excep that the Bitmap class allows you lower level access to the image...
1
2780
by: CompletelyUseless | last post by:
Hi, I'm having a lot of troubles trying to build a function to figure out the difference (in color) between two images. I've tried to build something similar with the ImageMagick API, but I've had many troubles compiling a DLL from the libraries. I am also unfamiliar with the .net libraries, but I've been reading up on MSDN. I know how...
2
2792
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a "Graphics" object to wrap around the image (!). It's not so simple as shown in most examples (where they have a simple image file and hard copy it into a...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.