473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

get bitmap from image

82
Expand|Select|Wrap|Line Numbers
  1.  private void timer1_Tick(object sender, EventArgs e) {
  2.  
  3.             Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image);
  4.  
  5.             if (Invert(m_Undo))
  6.                 this.Invalidate();
  7.  
  8.             pbLeftDetected.Image = m_Undo;
  9.         }
  10.  
  11.         public static bool Invert(Bitmap b) {
  12.             // GDI+ still lies to us - the return format is BGR, NOT RGB.
  13.             BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  14.  
  15.             int stride = bmData.Stride;
  16.             System.IntPtr Scan0 = bmData.Scan0;
  17.  
  18.             unsafe {
  19.                 byte* p = (byte*)(void*)Scan0;
  20.  
  21.                 int nOffset = stride - b.Width * 3;
  22.                 int nWidth = b.Width * 3;
  23.  
  24.                 for (int y = 0; y < b.Height; ++y) {
  25.                     for (int x = 0; x < nWidth; ++x) {
  26.                         p[0] = (byte)(255 - p[0]);
  27.                         ++p;
  28.                     }
  29.                     p += nOffset;
  30.                 }
  31.             }
  32.  
  33.             b.UnlockBits(bmData);
  34.  
  35.             return true;
  36.         }
Hi I am using this code to regularly convert a webcam image (pbLeftWebcam) into a bitmap (m_Undo) for use with the invert routine. But I get an error saying "Object reference not set to an instance of an object" when I run it. I realize that the difficulty is converting the image to a bitmap, but I can't think how to get around it. Anybody got any ideas?
thnx
Oct 16 '09 #1
17 7975
tlhintoq
3,525 Expert 2GB
Where do you get the error?
What line?
When the debug stops on the offending line you can hover the mouse over each variable to see which one is null.
Oct 16 '09 #2
tlhintoq
3,525 Expert 2GB
I will make one comment though... You can't assume that everything goes right in the world.

Expand|Select|Wrap|Line Numbers
  1. private void timer1_Tick(object sender, EventArgs e) {
  2.  
  3.             Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image);
Just because you have a timer ticking doesn't mean that you have an image from a Webcam. Hardware fails. You should start getting in the habit of presuming that everything will go wrong and coding ways around failure.

The camera could die... the USB cable could come undone... So there might not even be a webcam. Or it might not be on or working, so there could be a cam but no image.

Expand|Select|Wrap|Line Numbers
  1.  private void timer1_Tick(object sender, EventArgs e) {
  2.  if (pbLeftWebcam != null && pbLeftWebcam.Image != null)
  3. {
  4.             Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image);
  5.  
  6.  
Here we only try to work with the camera and the image after checking that there *is* a camera and there *is* an image
Oct 16 '09 #3
mrcw
82
thats a good point thanks, but I'm still left with the problem of converting an image to a bitmap
Oct 16 '09 #4
tlhintoq
3,525 Expert 2GB
@mrcw
What 'problem'? You've done it right in your code.
Assuming of course that the .Image property of pmLeftCam really is a standard Image and not some other kind of proprietary format from the webcam maker that they are *calling* image.
Oct 16 '09 #5
mrcw
82
thanks for your advice I thought I was going ga-ga. I'll do some more research on Trust webcams.
Oct 16 '09 #6
mrcw
82
Hi, I've checked that my webcams are standard, and the webcam bit works fine. The problem occurs when I try to convert the individual images to bmps.

If I replace the webcam with a bmp I can alter the bmp by using graphics, so that bit works fine too.

But with a webcam I keep on getting a runtime error saying "Does not refer to object instance"

The way I understand a webcam works it is a camera that takes about 30 pictures a second - because each picture is slightly different it gives the illusion of movement (a bit like a cartoon , but with a stream of camera pictures rather than drawings). So I I could find a way to lock my work to the pictures I would be able to work/alter them as bmps.

Anybody got any advice
Oct 19 '09 #7
tlhintoq
3,525 Expert 2GB
But with a webcam I keep on getting a runtime error saying "Does not refer to object instance"
What object is null when you get that error?
On which line does the code stop?
Oct 19 '09 #8
mrcw
82
Hi
I added the check code that you suggested

if (pbLeftWebcam != null && pbLeftWebcam.Image != null)

which checks the webcam is pugged in., but I get the error on

Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image);

I take it that the webcam takes 30 pictures a second, but my code is not synchronized with the webcam. When my code trys to convert thr image to a bmp the bmp is still being assembled, so the image is not there and the code runtime errors.
Oct 20 '09 #9
tlhintoq
3,525 Expert 2GB
Seems odd... I would have expected the Webcam.Image property to always hold an image once on: even if it is the previous frame while it is getting the new frame.

Since you are checking to see that the .Image is not null I'm thinking it must be something.

Please confirm for me what exactly is the error returned, which line of code the debugger stops on, and what object is reporting as null when the code stops.
Oct 20 '09 #10
mrcw
82
runtime error
Value cannot be null.
Parameter name: image

line it fails on is
Graphics g = Graphics.FromImage(m_Undo);
Oct 21 '09 #11
tlhintoq
3,525 Expert 2GB
@mrcw
Expand|Select|Wrap|Line Numbers
  1.  private void timer1_Tick(object sender, EventArgs e) {
  2.  
  3.             Bitmap m_Undo = new Bitmap(pbLeftWebcam.Image);
  4.  
  5.             if (Invert(m_Undo))
  6.                 this.Invalidate();
  7.  
  8.             pbLeftDetected.Image = m_Undo;
  9.         }
  10.  
  11.         public static bool Invert(Bitmap b) {
  12.             // GDI+ still lies to us - the return format is BGR, NOT RGB.
  13.             BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  14.  
  15.             int stride = bmData.Stride;
  16.             System.IntPtr Scan0 = bmData.Scan0;
  17.  
  18.             unsafe {
  19.                 byte* p = (byte*)(void*)Scan0;
  20.  
  21.                 int nOffset = stride - b.Width * 3;
  22.                 int nWidth = b.Width * 3;
  23.  
  24.                 for (int y = 0; y < b.Height; ++y) {
  25.                     for (int x = 0; x < nWidth; ++x) {
  26.                         p[0] = (byte)(255 - p[0]);
  27.                         ++p;
  28.                     }
  29.                     p += nOffset;
  30.                 }
  31.             }
  32.  
  33.             b.UnlockBits(bmData);
  34.  
  35.             return true;
  36.         }
You seem to be referring to a line that isn't in the code you supplied.
Oct 21 '09 #12
mrcw
82
sorry about that but I constantly trying new thng - that dont work.

Can I send you the complete project, that way I can't change anything and you could see beter where I'm going wrong.
Oct 21 '09 #13
tlhintoq
3,525 Expert 2GB
I really can't do that. Partly because I have my own coding deadlines that take precidence (I like paying my bills <laugh>). Partly because it opens the way for 100 other people to want to send me their projects to debug. But mostly because the best education in coding is trial and error and error and error and error. We learn more by doing than by any other means.

But I can make some suggestions.
  • Assume that everything is broken, or at least not ideal.
  • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
  • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
  • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
  • Check that folders exist, even if you just did a call to make it: You may not have permissions.
  • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
    • Put a breakpoint on line 1.
    • When the code stops there, walk through line by line with F-10.
    • Check the values of your assumptions.
  • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
  • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.

Graphics g = Graphics.FromImage(m_Undo);
Presumes that m_Undo must be good (not null). If that assumption fails so does your program. Get used to validating data and assumptions in your code if you want it to be robust. For example:
Expand|Select|Wrap|Line Numbers
  1. if (m_Undo != null)
  2. {
  3.    bool bSuccess = false;
  4.    // Do your thing here
  5.    bSuccess = true;
  6.    // Hurray, your thing worked!
  7.  
  8.    if (bSuccess)
  9.    {
  10.       // Then do this other thing if it worked
  11.    }
  12.    else
  13.    {
  14.       // Then do the failure recovery part / user failure message
  15.    }
  16.  
  17.    return bSuccess; // If you want the calling method to know if it worked.
  18. }
Oct 21 '09 #14
mrcw
82
Thanks for the help you have given me. As you may have guessed I'm not a c# programmer, nor do I profess to be, but I can tell you exactly how a radar works and how radar information is sent around Europe and UK. Thanks again. This c# blows my mind.
Oct 22 '09 #15
tlhintoq
3,525 Expert 2GB
@mrcw
I was crypto in the Army, so I'm right there with ya.
Oct 22 '09 #16
mrcw
82
The world has certainly changed since then, it was all cold war stuff. At least when I was "in" we knew who the enemy was. I feel sorry for guys these days, the enemy could be anybody.

Anyway whats wrong with this?
public myCamLeft as ICam;
I get a red line underneath "as" and ";"
Oct 22 '09 #17
tlhintoq
3,525 Expert 2GB
@mrcw
Hover the mouse over the read underlines and you will get a tooltip popup with the reason
Oct 22 '09 #18

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

Similar topics

0
by: gana | last post by:
I'm converting an EMF to BMP. Image is coming properly except that background color of the bMP is set to black. I'm saving EMF to a stream and craeting BMP from stream. One more thing I tried is...
1
by: dcudev.lcr | last post by:
I am loading a 4X5 picturebox matrix using a bitmap file template. As each cell in the matrix is being loaded, I append a text string using the DrawString function to complete the cell's image. ...
1
by: Joseff | last post by:
I have this Bitmap object declared and outputted to the browswer this way: invoiceBMP.Save(Response.OutputStream, ImageFormat.Gif); However, the result is that the existing static controls of...
3
by: Niels Jensen | last post by:
I have a bitmap image that's around 640 x 480 pixels. I want to add some ..gif or .png files to the image before displaying it in my application. Is there a way of doing this? Cheers Niels
0
by: J L | last post by:
The VBHelper website has very good code for printing an image of the current form. Part of this code is a routine called GetFormImage which returns a bitmap of the form. What I do not know how to...
1
by: makdtripper | last post by:
I create a bitmap image. I can draw and save it successfulyl...i need to know how to resize the bitmap when i add more and more drawings...or how to add scroll bars to a large created bitmap. My...
2
by: ajay_itbhu | last post by:
Hi everyone, I want to read the pixel values of 2 similar images in bitmap format like 2 continuous frame of a video for calculating the median. But i dont know how to read the pixel values of...
1
by: icepick72 | last post by:
On an academic note, I want to copy a Graphic to an Image (Bitmap). I have the Graphic object but not the origin image from which it originates; this is because I'm overriding the PrintDocument...
0
by: ProtossLee | last post by:
Hi, I am make a project to draw bitmap image. Pixel intensities are calculated from data received from instruments. Problem is, sometimes I get wrong data points, such as: correct data should be...
1
by: swetha | last post by:
HI, I am very new to C++.Using C++ I am reading one bitmap image.Now,I am trying to zooming that image.Can any one tell me which function have to use for this.Or can any one send some sample code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.