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: -
-
public static Bitmap Change(Bitmap LeftBitamp, Bitmap RightBitamp)
-
{
-
Bitmap LeftB = LeftBitamp;
-
Bitmap RightB = RightBitamp;
-
// the return format is BGR.
-
// This is where I lock the data so I can declare a pointer to them.
-
// bmDataLeft for the LeftBitmap, bmDataRight for the RightBitmap,
-
BitmapData bmDataLeft = LeftB.LockBits(new Rectangle(0, 0, LeftB.Width, LeftB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
-
BitmapData bmDataRight = RightB.LockBits(new Rectangle(0, 0, RightB.Width, RightB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
-
-
// The normal procedure for pointer to an image variables
-
int strideL = bmDataLeft.Stride;
-
IntPtr Scan0L = bmDataLeft.Scan0;
-
-
int strideR = bmDataRight.Stride;
-
IntPtr Scan0R = bmDataRight.Scan0;
-
-
unsafe
-
{
-
#region 3 bytes
-
// These are my actual data I will do operations on.
-
byte* pL = (byte*) (void*) Scan0L;
-
byte* pR = (byte*) (void*) Scan0R;
-
// These are for the correct indexing.
-
// There shouldn't be a problem here.
-
int nOffsetLeft = strideL - LeftB.Width*3;
-
int nOffsetRight = strideR - RightB.Width*3;
-
int nWidthLeft = LeftB.Width*3;
-
int nWidthRight = RightB.Width*3;
-
-
// Looping through all pixels.
-
for (int y = 0; y < LeftB.Height; ++y)
-
{
-
for (int x = 0; x < nWidthLeft; ++x)
-
{
-
// This is the line that is not functioning properly
-
// Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
-
pL[0] = (byte)(pR[0]);
-
-
// But strangely this one does work! :
-
// pL[0] = (byte) (pL[0]+50);
-
// Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
-
-
-
++pL;
-
++pR;
-
}
-
pL += nOffsetLeft;
-
pR += nOffsetRight;
-
}
-
-
#endregion
-
-
-
}
-
-
LeftB.UnlockBits(bmDataLeft);
-
RightB.UnlockBits(bmDataRight);
-
LeftB.Save("LeftOne.jpg");
-
RightB.Save("RightOne.jpg");
-
-
return LeftB;
-
}
-
-
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
6 2138
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). -
-
for (int y = 0; y < LeftB.Height; ++y)
-
{
-
for (int x = 0; x < LeftB.Width; ++x)
-
{
-
// This is the line that is not functioning properly
-
// Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
-
pL[0] = (byte)(pR[0]);
-
-
// But strangely this one does work! :
-
// pL[0] = (byte) (pL[0]+50);
-
// Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
-
-
-
pL+=3;
-
pR+=3;
-
}
-
pL += nOffsetLeft;
-
pR += nOffsetRight;
-
}
-
-
}
-
-
I hope this helps somebody else as well.
Regards,
Aram Azhari
Plater 7,872
Recognized Expert Expert
Was this something you could not do with the Bitmap class and it's GetPixel()/SetPixel() functions?
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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'...
|
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
|
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);
|
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?
| |
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()",...
|
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...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |