?
Solved

Using CameraCaptureDialog() in C#

Posted on 2009-04-16
45
Medium Priority
?
6,212 Views
Last Modified: 2013-12-21
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

0
Comment
Question by:bquinn10
  • 26
  • 11
  • 7
44 Comments
 

Author Comment

by:bquinn10
ID: 24161494
after debugging it appears that the program will perform everything before the line....

if (DialogResult.OK == captureImage.ShowDialog())
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24161538
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
 

Author Comment

by:bquinn10
ID: 24161549
i did dispose of it at the end of the action listener
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:bquinn10
ID: 24161553
line 141
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24161555
Sorry I just saw you are doing that ...
0
 

Author Comment

by:bquinn10
ID: 24161596
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24161608
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
 

Author Comment

by:bquinn10
ID: 24161685
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
 

Author Comment

by:bquinn10
ID: 24161896
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
 

Author Comment

by:bquinn10
ID: 24161908
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
 

Author Comment

by:bquinn10
ID: 24168394
does anyone have any solutions to this?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24168557
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
 

Author Comment

by:bquinn10
ID: 24168635
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
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 400 total points
ID: 24168696
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
 

Author Comment

by:bquinn10
ID: 24168907
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
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24168991
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
 

Author Comment

by:bquinn10
ID: 24169099
yes i put codecs=null and the problem still occurs
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24169195
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
 

Author Comment

by:bquinn10
ID: 24169515
no luck... put GC.Collect() and GC.WaitForPendingFinalizers() right after the problem code and same results occured
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24169784
How about


 using ( RasterCodecs codecs = new RasterCodecs() )
{
  ... Do stuff
}
0
 

Author Comment

by:bquinn10
ID: 24169851
where would i put that?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24170153
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
 

Author Comment

by:bquinn10
ID: 24184009
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
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24184042
exactly :)
any news from RasterCodecs ?
0
 

Author Comment

by:bquinn10
ID: 24184369
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
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24186574
Looks like RasterCodecs codecs = new RasterCodecs()  needs to be moved to being a Class member

0
 

Author Comment

by:bquinn10
ID: 24186601
tried that... no love
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24186623
RasterCodecs.Shutdown();
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24186639
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
 

Author Comment

by:bquinn10
ID: 24186658
na there is no method to release this object from memory
0
 

Author Comment

by:bquinn10
ID: 24186674
RasterCocs.Shutdown(); is not a known method
0
 

Author Comment

by:bquinn10
ID: 24186685
RasterCodecs.Shutdown(); ***
0
 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 400 total points
ID: 24186692
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24186695
Its a Static
0
 

Author Comment

by:bquinn10
ID: 24186716
hmm ok it wasnt coming up in the intellisense... so let me try that
0
 

Author Comment

by:bquinn10
ID: 24186760
ok you were looking at the api for regular win32 sdk.. im working with the mobile sdk
0
 

Author Comment

by:bquinn10
ID: 24186769
so those wont work
0
 

Accepted Solution

by:
bquinn10 earned 0 total points
ID: 24187513
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
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24187604
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
 

Author Comment

by:bquinn10
ID: 24187766
and my initial idea about the real issue was?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24187887
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
 

Author Comment

by:bquinn10
ID: 24188000
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
 

Author Comment

by:bquinn10
ID: 24188007
w/e the case may be, thanks for those who helped
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24188285
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question