Using CameraCaptureDialog() in C#

So I am developing a test app for a windows mobile device, that is running Windows Mobile 6.  In my application, I have a button that when a user presses it, the digital camera on the device is activated so that the user can take a picture, after the user takes a picture, they are asked if they are satisfied with the picture, and if they are the image is convert to Jpeg2000 according to a specific target file size specified by the user.  The problem that is occuring is when I run this app through more then once.  Ie. I can start the app, hit the button to bring up the camera, take a picture, and save/convert is succesfully, then when i press the button again (to take another picture) the application just freezes and quits, and I am not sure why... the code for the action listener for the button is posted below.
private void captureButton_Click(object sender, EventArgs e)
        {
            object cameraEnabled = Microsoft.WindowsMobile.Status.SystemState.GetValue(Microsoft.WindowsMobile.Status.SystemProperty.CameraPresent);
            if (null != cameraEnabled && 0 == (int)cameraEnabled)
            {
                MessageBox.Show("The camera is disabled", this.Text,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }
            try
            {
                filesize = Convert.ToDouble(filesizepulldown.Text) * 1024;
                CameraCaptureDialog captureImage = new CameraCaptureDialog();
                captureImage.Owner = this;
                captureImage.StillQuality = CameraCaptureStillQuality.Default;
                imageprogress.Value = 0;
                //makes sure saving procedues are not executed unless a picture is taken
                if (DialogResult.OK == captureImage.ShowDialog())
                {
                    GpsPosition position = gps.GetPosition();
                    //gets the necessary data for the image filename
                    string folderformat = "M_dd_yyyy";
                    string filenameformat = "MM-dd-yy_HHmmss";
                    String foldername = DateTime.Now.ToString(folderformat);
                    String imgname = DateTime.Now.ToString(filenameformat);
                    //gets the necessary data for the image log file
                    string format = "M/dd/yyyy HH:mm:ss";
                    String localtime = DateTime.Now.ToString(format);
                    String UTCtime = DateTime.UtcNow.ToString(format);
                    String imagefilename = imgname + "_Image#" + imgnumber + ".Jp2";
                    String Latitude = position.Latitude.ToString();
                    String Longitude = position.Longitude.ToString();
                    String nSatView = position.SatellitesInViewCount.ToString();
                    String nSatTrack = position.SatelliteCount.ToString();
                    RasterCodecs codecs = new RasterCodecs();
                    srcFileName = captureImage.FileName;
 
                    if (MessageBox.Show("Are you satisfied with the image you just captured?", "Image Verification", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        codecs.Options.Jpeg2000.Load.Jp2Resolution = new Size(800, 800);
 
                        imageprogress.Value = 20;
                        // load the Jpeg2000 image.
                        IRasterImage img = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1);
                        // set the Jpeg2000 save options 
                        codecs.Options.Jpeg2000.Save.CodeBlockHeight = 4;
                        codecs.Options.Jpeg2000.Save.CodeBlockWidth = 8;
                        codecs.Options.Jpeg2000.Save.CompressionControl = CodecsJpeg2000CompressionControl.TargetSize;
                        codecs.Options.Jpeg2000.Save.CompressionRatio = 15.0f;
                        codecs.Options.Jpeg2000.Save.DecompositionLevels = 5;
                        codecs.Options.Jpeg2000.Save.DerivedBaseExponent = 8;
                        codecs.Options.Jpeg2000.Save.DerivedBaseMantissa = 2;
                        codecs.Options.Jpeg2000.Save.DerivedQuantization = true;
                        codecs.Options.Jpeg2000.Save.ErrorResilienceSymbol = false;
                        codecs.Options.Jpeg2000.Save.GuardBits = 4;
                        codecs.Options.Jpeg2000.Save.ImageAreaHorizontalOffset = 0;
                        codecs.Options.Jpeg2000.Save.ImageAreaVerticalOffset = 0;
                        codecs.Options.Jpeg2000.Save.PredictableTermination = false;
                        codecs.Options.Jpeg2000.Save.ProgressingOrder = CodecsJpeg2000ProgressionsOrder.PositionComponentResolutionLayer;
                        codecs.Options.Jpeg2000.Save.ReferenceTileHeight = 240;
                        codecs.Options.Jpeg2000.Save.ReferenceTileWidth = 480;
                        codecs.Options.Jpeg2000.Save.RegionOfInterest = CodecsJpeg2000RegionOfInterest.UseOptionRectangle;
                        codecs.Options.Jpeg2000.Save.RegionOfInterestRectangle = new Rectangle(0, 0, img.Width / 2, img.Height / 2);
                        codecs.Options.Jpeg2000.Save.ResetContextOnBoundaries = false;
                        codecs.Options.Jpeg2000.Save.SelectiveAcBypass = false;
                        codecs.Options.Jpeg2000.Save.TargetFileSize = (long)filesize;
                        codecs.Options.Jpeg2000.Save.TerminationOnEachPass = false;
                        codecs.Options.Jpeg2000.Save.TileHorizontalOffset = 0;
                        codecs.Options.Jpeg2000.Save.TileVerticalOffset = 0;
                        codecs.Options.Jpeg2000.Save.UseColorTransform = true;
                        codecs.Options.Jpeg2000.Save.UseEphMarker = false;
                        codecs.Options.Jpeg2000.Save.UseRegionOfInterest = false;
                        codecs.Options.Jpeg2000.Save.UseSopMarker = false;
                        codecs.Options.Jpeg2000.Save.VerticallyCausalContext = false;
 
                        String filenamestr = foldername + "\\" + imgname + "_Image#" + imgnumber + ".Jp2";
                        destFileName = destFolder + filenamestr;
                        imgnumber = imgnumber + 1;
                        imageprogress.Value = 55;
 
                        //creates "date" folder if it doesnt exist and then saves file
                        if (Directory.Exists(destFolder + foldername))
                        {
                            codecs.Save(img, destFileName, RasterImageFormat.Jp2, img.BitsPerPixel, 1, CodecsSavePageMode.Overwrite);
                            imageprogress.Value = 75;
                        }
                        else
                        {
                            Directory.CreateDirectory(destFolder + foldername);
                            codecs.Save(img, destFileName, RasterImageFormat.Jp2, img.BitsPerPixel, 1, CodecsSavePageMode.Overwrite);
                            imageprogress.Value = 75;
                        }
                        //creates the necessary strings for the log file, and then writes to the log file
                        String logtitlestring = "\"System Time (Local)\",\"GPS Time (UTC)\",\"Image Identifier\",\"Latitude (deg)\",\"Longitude\",\"nSatsInView\",\"nSatsTracked\",";
                        String imageloginfo = "\"" + localtime + "\"," + "\"" + UTCtime + "\"," + "\"" + imagefilename + "\"," + "\"" + Latitude + "\"," + "\"" + Longitude + "\"," + "\"" + nSatView + "\"," + "\"" + nSatTrack + "\",";
                        if (File.Exists(@"\My Documents\imagelogfile.txt"))
                        {
                            TextWriter logfile = new StreamWriter(@"\My Documents\imagelogfile.txt", true);
                            logfile.WriteLine(imageloginfo);
                            logfile.Close();
                            imageprogress.Value = 100;
                        }
                        else
                        {
                            TextWriter logfile = new StreamWriter(@"\My Documents\imagelogfile.txt");
                            logfile.WriteLine(logtitlestring);
                            logfile.WriteLine(imageloginfo);
                            logfile.Close();
                            imageprogress.Value = 100;
                        }
 
                        //deletes original file taken by picture
                        //File.Delete(srcFileName);
                    }
                    else
                    {
                        File.Delete(srcFileName);
                        MessageBox.Show("Your image has been deleted, press \"Capture Image\" to take another picture", "Image Message");
                    }
                }
            }
            catch (FormatException ex)
            {
                MessageBox.Show("You must pick a target file size", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
 
            catch (RasterException ex)
            {
                MessageBox.Show(string.Format("Cannot save: {0}, error code: {1}", srcFileName, ex.Code));
            }
            catch (OutOfMemoryException ex)
            {
                // There is not enough memory to save the image or video.
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
                MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Cannot save: {0}, message: {1}", srcFileName, ex.Message));
            }
            captureImage.Dispose();
        }

Open in new window

bquinn10Asked:
Who is Participating?
 
bquinn10Connect With a Mentor Author Commented:
ok so i solved the problem.  I think the application was getting confused with the RasterCodecs and the CameraCaptureDialog running at the same time for some reason.  I changed the location of captureImage.Dispose() to the indicated spot below and the problem was fixed....
  if (DialogResult.OK == captureImage.ShowDialog())
                {
                    GpsPosition position = gps.GetPosition();
                    //gets the necessary data for the image filename
                    string folderformat = "M_dd_yyyy";
                    string filenameformat = "MM-dd-yy_HHmmss";
                    String foldername = DateTime.Now.ToString(folderformat);
                    String imgname = DateTime.Now.ToString(filenameformat);
                    //gets the necessary data for the image log file
                    string format = "M/dd/yyyy HH:mm:ss";
                    String localtime = DateTime.Now.ToString(format);
                    String UTCtime = DateTime.UtcNow.ToString(format);
                    String imagefilename = imgname + "_Image#" + imgnumber + ".Jp2";
                    String Latitude = position.Latitude.ToString();
                    String Longitude = position.Longitude.ToString();
                    String nSatView = position.SatellitesInViewCount.ToString();
                    String nSatTrack = position.SatelliteCount.ToString();
                    RasterCodecs codecs = new RasterCodecs();
                    srcFileName = captureImage.FileName;
                    captureImage.Dispose();
 
                    if (MessageBox.Show("Are you satisfied with the image you just captured?", "Image Verification", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        imageprogress.Value = 20;
                        
                        // load the Jpeg2000 image.
                        
                        IRasterImage img = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1);
                        codecs.Options.Jpeg2000.Save.CompressionControl = CodecsJpeg2000CompressionControl.TargetSize;
                        codecs.Options.Jpeg2000.Save.TargetFileSize = (long)filesize;

Open in new window

0
 
bquinn10Author Commented:
after debugging it appears that the program will perform everything before the line....

if (DialogResult.OK == captureImage.ShowDialog())
0
 
williamcampbellCommented:
CameraCaptureDialog captureImage = new CameraCaptureDialog();

there needs to be a matching dispose or close for this new

I think you are running two capture controls at once which is probably bad
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
bquinn10Author Commented:
i did dispose of it at the end of the action listener
0
 
bquinn10Author Commented:
line 141
0
 
williamcampbellCommented:
Sorry I just saw you are doing that ...
0
 
bquinn10Author Commented:
its cool, any other ideas as to why this is happening? cuz i have been staring at this for a while and i cant think of naything
0
 
williamcampbellCommented:
What if you take

CameraCaptureDialog captureImage = new CameraCaptureDialog();

out of the handler and make it a member of your class that is accessed inside the handler

and see if the same thing happens

(the garbage collector may be taking its time)
0
 
bquinn10Author Commented:
ok so i put CameraCaptureDialog captureImage=new CamerCaptureDialog() at the top right below the public class Form1:Form in my code and the same exact thing happened
0
 
bquinn10Author Commented:
may i also add, when i run this program from visual studio on the device, when i run through the program the second time i hit the button, a window pops up in visual studio saying that the remote connection to the device has been lost, and the program quits on the device and the debuggers ends in visual studio
0
 
bquinn10Author Commented:
im leaving the office now, if anyone has any solutions please post them and I will try them out first thing tomorrow morning at 8 am
0
 
bquinn10Author Commented:
does anyone have any solutions to this?
0
 
alexey_gusevCommented:
have you tried to do something like that:

                CameraCaptureDialog captureImage = new CameraCaptureDialog();
                captureImage.Owner = this;
                //makes sure saving procedues are not executed unless a picture is taken
                try
                {
                     if (DialogResult.OK == captureImage.ShowDialog())
                     {
                           MessageBox.Show("Taken");
                    }
                }
                 catch(Exception e)
                 {
                 }
                 finally
                 {
                        captureImage.Dispose();
                 }

what I'm getting at is try to test just CameraDialog, without anything else. to eliminate possible memory corruption (e.g. with codec)

0
 
bquinn10Author Commented:
ok i tried your suggestion, and it in fact did not freeze or quit the application.... so what should i do now, that codec part is the whole reason I am writing this application.  So that i can convert the image using that codec object
0
 
alexey_gusevConnect With a Mentor Commented:
well, I would suggest starting to debug it, ie commenting out all the code that uses codecs except the line

RasterCodecs codecs = new RasterCodecs();

btw, where do you release it (if it provides any Dispose() method). anyway, you might try to set it to null somewhere at the end.

if this works, then try to uncomment your code line by line (or at least by logical chunks) and see what happens. I don't know what RasterCodecs class does, so it is pretty possible it leaks memory or doesn't release something etc
0
 
bquinn10Author Commented:
ok so i found the exact line that causes this freeze.  and i looked for a dispose or close function for the RasterCodecs class and there was nothing...maybe i'll have to contact leadtools who is the developer of this rastercodecs api
0
 
alexey_gusevCommented:
sounds like a plan :)

have you tried to assign a null to codecs variable? I guess garbage collector gets at it after some time, but codecs might use some COM objects or whatever and don't release them until forced to do so. Just guessing...
0
 
bquinn10Author Commented:
yes i put codecs=null and the problem still occurs
0
 
alexey_gusevCommented:
ok, just as a matter of curiosity, could you try to force GC to collect immediately, like here:

http://www.codeguru.com/columns/dotnettips/article.php/c7047


0
 
bquinn10Author Commented:
no luck... put GC.Collect() and GC.WaitForPendingFinalizers() right after the problem code and same results occured
0
 
williamcampbellCommented:
How about


 using ( RasterCodecs codecs = new RasterCodecs() )
{
  ... Do stuff
}
0
 
bquinn10Author Commented:
where would i put that?
0
 
williamcampbellCommented:
Move it as shown
                    srcFileName = captureImage.FileName;
 
                    if (MessageBox.Show("Are you satisfied with the image you just captured?", "Image Verification", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        // Move it inside here
                        using ( RasterCodecs codecs = new RasterCodecs() )
                        {
                            codecs.Options.Jpeg2000.Load.Jp2Resolution = new Size(800, 800);
 
                            imageprogress.Value = 20;
                            // load the Jpeg2000 image.
                            IRasterImage img = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1);
 
                            .......... snip ..... 
 
                            //creates "date" folder if it doesnt exist and then saves file
                            if (Directory.Exists(destFolder + foldername))
                            {
                                codecs.Save(img, destFileName, RasterImageFormat.Jp2, img.BitsPerPixel, 1, CodecsSavePageMode.Overwrite);
                                imageprogress.Value = 75;
                            }
                            else
                            {
                                Directory.CreateDirectory(destFolder + foldername);
                                codecs.Save(img, destFileName, RasterImageFormat.Jp2, img.BitsPerPixel, 1, CodecsSavePageMode.Overwrite);
                                imageprogress.Value = 75;
                            }            
                        }
                    }

Open in new window

0
 
bquinn10Author Commented:
na no love there... that didnt work either i got some error saying that "type used in using statement must be implicitly convertible to 'System.IDisposable'"
0
 
alexey_gusevCommented:
exactly :)
any news from RasterCodecs ?
0
 
bquinn10Author Commented:
I was working with a tech rep on friday, but he couldnt solve it.  I had to leave early from work so we will continue working on the issue today.... does anyone have any other suggestions I could try?
0
 
williamcampbellCommented:
Looks like RasterCodecs codecs = new RasterCodecs()  needs to be moved to being a Class member

0
 
bquinn10Author Commented:
tried that... no love
0
 
williamcampbellCommented:
RasterCodecs.Shutdown();
0
 
williamcampbellCommented:
Heres a snippet
         RasterCodecs.Startup(); 
         RasterCodecs codecs = new RasterCodecs(); 
 
         string srcFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1.cmp"; 
         string dstFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1_test.jpg"; 
 
         RasterImage image = codecs.Load(srcFileName); 
         codecs.Save(image, dstFileName, RasterImageFormat.Jpeg, 0); 
 
         // Clean up 
         image.Dispose(); 
         codecs.Dispose(); 
         RasterCodecs.Shutdown(); 

Open in new window

0
 
bquinn10Author Commented:
na there is no method to release this object from memory
0
 
bquinn10Author Commented:
RasterCocs.Shutdown(); is not a known method
0
 
bquinn10Author Commented:
RasterCodecs.Shutdown(); ***
0
 
williamcampbellCommented:
Its a Static
0
 
bquinn10Author Commented:
hmm ok it wasnt coming up in the intellisense... so let me try that
0
 
bquinn10Author Commented:
ok you were looking at the api for regular win32 sdk.. im working with the mobile sdk
0
 
bquinn10Author Commented:
so those wont work
0
 
alexey_gusevCommented:
well, taking into account the amount of time people spent on helping bgquinn10 in solving the issue I disagree with simple closing ;)

moreover, his/her initial idea about the real issue was 100% wrong, so I think the answer given here deserves at least Assisted Solution marks.
0
 
bquinn10Author Commented:
and my initial idea about the real issue was?
0
 
alexey_gusevCommented:
don't get it too hard :)

I think from your original post that initial idea was that CameraCaprureDialog is to blame, which turned out is partially true, but not really as it clashed with codecs object.

I don't mind if you close the question or assign some points, really, that's up to you :)
will I bother the next time? well, I'm not sure...
0
 
bquinn10Author Commented:
this site is absurd... all people care about is the dumbass points and whether not a question is closed properly rather then offering their expertise on a topic so that some1 can get the help they need and move on with their life... what do those points get u... a t-shirt or plaque or something???
0
 
bquinn10Author Commented:
w/e the case may be, thanks for those who helped
0
 
williamcampbellCommented:
We get the warm fuzzy feeling that we did some good in the world by helping programmers who are blocked. We often learn stuff along the way. In this case we now know that Mixing that Codec and Capture is a no no.

Hence we usually like to PAQ these questions so the next person to hit this problem has a fix. Oftentimes if you google a problem it ends up here at EE.

Most people usually give out points even when they figure the thing out by themselves. Sometimes to share the answer and sometimes because airing a problem for others can help you see things you wouldn't otherwise see.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.