Resolving unhandled exception problems with CF 3.5 Bitmap (Stream) in asp.net

Source: Internet
Author: User

VS2008 development environment, when programming wm sockets under. NET CF 3.5

Bitmap BMP = Unhandled exception occurred at New Bitmap (Stream)

By groping, I find that the problem persists regardless of how the object is set to null and Dispose.

The reason, the individual guess is bitmap instantiation of itself will open MemoryStream flow, so then use MemoryStream is very easy to leak memory

Therefore, it is recommended that when combined with bitmap, replace the MemoryStream with FileStream or other workarounds and reclaim the memory in a timely manner.

The following are examples of using references:

The main function is: Photo upload.

The code is as follows Copy Code

View Code public class Photohelper
{
BaseForm _form; Photo Upload Form
TextBox _txtfilename; Show Photo Path
PictureBox _picphoto; Show photo thumbnails

Public Photohelper (BaseForm form,textbox txtfilename,picturebox Picphoto)
{
_form = form;
_txtfilename = Txtfilename;
_picphoto = Picphoto;
_picphoto.image = new Bitmap (300, 350);

}

     
 

       #region Camera logic
        public void Takephoto ()
        {
            //Photo-----------------------------         
            cameracapturedialog CCD = new CameraCaptureDialog ();
            CCD. Mode = Cameracapturemode.still;
            CCD. Owner = _form;
            CCD. Title = "Photograph";
            CCD. Resolution = new Size (1600, 1200);
            CCD. stillquality = Cameracapturestillquality.normal;

            if (CCD. ShowDialog () = = DialogResult.OK)
            {
               //will be photographed in thumbnail display
                Imagechangedargs args = new Imagechangedargs (CCD. FileName);
                using (Graphics g = Graphics.fromimage (_picphoto.image))
                 {

                     g.drawimage (args. Image,
                         New Rectangle (0, 0, _picphoto.image.width, _picphoto.image.height),
                         New Rectangle (0, 0, args. Image.width, args. Image.height),
                         GraphicsUnit.Pixel);

}
Args. Dispose ();
args = null;

_txtfilename.text = CCD. FileName;

}
Ccd. Dispose ();
CCD = NULL;
}
#endregion

#region Photo Upload logic

public void Photosave ()
{
if (string. IsNullOrEmpty (_txtfilename.text))
{
Global.showmsg ("No photos to upload");
Return
}

string prompt = "Confirm upload photo?" ";
if (global.showyesnomsg (prompt) = = dialogresult.no)
Return


Convert photo to byte[]
Byte[] B = filetobytes (_txtfilename.text);

#region Todo will byte[] upload the server via WebService, please add your own code here
Commandandtaskidobj commandobj = _form.commandandtaskidobj;
Wsrmsg msg = Global.getservice (). Upfile (Commandobj.username, Commandobj.commandid, Commandobj.taskid, B, _txtfilename.text);
if (Msg. Flag = 0)
//{
b = null;
throw new Exception (Msg. EMSG);
//}
#endregion

Global.showmsg ("Photo upload success");


_txtfilename.text = string. Empty;
b = null;

}
#endregion

Static byte[] Filetobytes (string sfilepath)
{
FileStream inFile = null;
Try
{
Byte[] BinaryData;
InFile = new FileStream (Sfilepath, FileMode.Open, FileAccess.Read);
BinaryData = new Byte[infile.length];
Long bytesread = Infile.read (binarydata, 0, (int) infile.length);
return binarydata;
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
if (inFile!= null)
{
Infile.close ();
Infile.dispose ();
}
InFile = null;
}
}

internal void Photodispose ()
{
throw new NotImplementedException ();
}
}

public class Imagechangedargs:eventargs, IDisposable
{
Private Bitmap image;
<summary>
Used to create the thumbnail
</summary>
private stream stream;
Public Imagechangedargs (String imagefilepath)
{

Try
{
This.stream = new FileStream (Imagefilepath, FileMode.Open, FileAccess.Read);
This.image = new Bitmap (this.stream);
}
catch (Exception ex)
{
This.image = null;
if (This.stream!= null)
{
This.stream.Close ();
This.stream = null;
}
}
}

        public Bitmap Image
        {
            Get
             {
                 return this.image;
           }
       }

Public Stream Stream
{
Get
{
return this.stream;
}
}

#region IDisposable Members

public void Dispose ()
{
if (this.image!= null)
This.image.Dispose ();
This.image = null;
if (This.stream!= null)
{
This.stream.Close ();
This.stream.Dispose ();
}
This.stream = null;
}

#endregion
}

As can be seen from the last example, the MemoryStream class is basically replaced by FileStream.

When a large picture is loaded, the exception exception is not reported as long as the memory allows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.