Problem : Attempted to read or write protected memory ...

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
i have an application which can view or save images as tasks document.
my problem is that when user want to save image to local drive, im facing this error :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

here is my code :

<div style="color:Black;background-color:White; <pre>
ImageCodecInfo codec = <span style="color:Blue; this.lkuFormat.EditValue <span style="color:Blue; as ImageCodecInfo;
Bitmap bmp = (<span style="color:Blue; this._croppedImage != <span style="color:Blue; null ? <span style="color:Blue; this._croppedImage : <span style="color:Blue; this._sourceImage);
<span style="color:Blue; this.saveFileDialog1.Filter = <span style="color:Blue; string.Format(<span style="color:#A31515; "{0}|*.{1}", codec.CodecName, codec.FilenameExtension);

<span style="color:Blue; if (<span style="color:Blue; this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
<span style="color:Blue; if (<span style="color:Blue; this._processor.SaveFile(codec, bmp, <span style="color:Blue; this.saveFileDialog1.FileName, trkQuality.Value))
{
MessageBox.Show(<span style="color:#A31515; "Image saved successfully!", <span style="color:#A31515; "Operation complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
<span style="color:Blue; else
{
MessageBox.Show(<span style="color:#A31515; "Unable to save file.", <span style="color:#A31515; "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
[/code]

<br/>
and here is ImageProcessor.SaveFile method :

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; bool SaveFile(ImageCodecInfo codec, Bitmap img, <span style="color:Blue; string filename, <span style="color:Blue; int quality)
{
<span style="color:Blue; try
{
<span style="color:Green; // Create parameters
EncoderParameters ep = <span style="color:Blue; new EncoderParameters(1);

<span style="color:Green; // Set quality
ep.Param[0] = <span style="color:Blue; new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

<span style="color:Green; // Save
img.Save(filename, codec, ep);

<span style="color:Blue; return <span style="color:Blue; true;
}
<span style="color:Blue; catch (Exception e)
{
System.Diagnostics.Debug.Fail(<span style="color:#A31515; "Error saving file", e.Message);
<span style="color:Blue; return <span style="color:Blue; false;
}
}
[/code]

<br/>
the funny thing is the same code works in my sample application but not works in main application!
can anybody help me ?
thanks in advance <hr class="sig http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

View the full article
 
Back
Top