C#窗体上传图片失败(显示图片正在被另一进程使用。。。。)
//选择图片privatevoidaddImage(){OpenFileDialogopenFileDialog=newOpenFileDialog();Bitmapbm...
//选择图片
private void addImage()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
Bitmap bmp = null;
try
{
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = true;
openFileDialog.Filter = "PDF files (*.png)|*.png|PDF files (*.pdf)|*.pdf|PDF files(*.jpg)|*.jpg";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
imageAddress = openFileDialog.FileName;
bmp=new Bitmap(openFileDialog.FileName);
//把图片copy到显示器里面,不copy的话就不能显示报异常
Clipboard.SetImage(bmp);
this.pictureBox1.Image = Clipboard.GetImage();
Clipboard.Clear();
}
}
catch (Exception ex)
{
imageAddress = "";
MessageBox.Show("上传图片的格式不正确!或者图片已经损坏!", "错误提示", MessageBoxButtons.OKCancel);
}
finally
{
//释放资源
openFileDialog.Dispose();
bmp.Clone();
bmp.Dispose();
}
}
private void uploadImageBT_Click(object sender, EventArgs e)
{
try
{
if (imageAddress == "")
{
if (MessageBox.Show("没有图片,马上添加?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
this.addImage();
}
}
else
{
//拿到原来图片(***************************出现错误!!!!!)
FileStream fs = File.Open(@imageAddress, FileMode.OpenOrCreate, FileAccess.Read);
//调用插入数据库的方法
method.InsertInto_img(imageAddress, fs);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示", MessageBoxButtons.OKCancel);
}
finally
{
//释放资源
//bmp.Dispose();
//tImage.Dispose();
}
} 展开
private void addImage()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
Bitmap bmp = null;
try
{
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = true;
openFileDialog.Filter = "PDF files (*.png)|*.png|PDF files (*.pdf)|*.pdf|PDF files(*.jpg)|*.jpg";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
imageAddress = openFileDialog.FileName;
bmp=new Bitmap(openFileDialog.FileName);
//把图片copy到显示器里面,不copy的话就不能显示报异常
Clipboard.SetImage(bmp);
this.pictureBox1.Image = Clipboard.GetImage();
Clipboard.Clear();
}
}
catch (Exception ex)
{
imageAddress = "";
MessageBox.Show("上传图片的格式不正确!或者图片已经损坏!", "错误提示", MessageBoxButtons.OKCancel);
}
finally
{
//释放资源
openFileDialog.Dispose();
bmp.Clone();
bmp.Dispose();
}
}
private void uploadImageBT_Click(object sender, EventArgs e)
{
try
{
if (imageAddress == "")
{
if (MessageBox.Show("没有图片,马上添加?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
this.addImage();
}
}
else
{
//拿到原来图片(***************************出现错误!!!!!)
FileStream fs = File.Open(@imageAddress, FileMode.OpenOrCreate, FileAccess.Read);
//调用插入数据库的方法
method.InsertInto_img(imageAddress, fs);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "错误提示", MessageBoxButtons.OKCancel);
}
finally
{
//释放资源
//bmp.Dispose();
//tImage.Dispose();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询