그래서 버튼을 클릭하면 그림 편집이 폴더 파일에서 이미지를 가져 오는 간단한 프로젝트를 만들었지 만 이미지가 포함 된 폴더를 삭제하고 싶을 때 오류가 발생합니다. 다음과 같은 코드
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
pictureEdit1.Image = Image.FromFile(pathx);
}
private void button2_Click(object sender, EventArgs e)
{
string dir = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage";
try {
if (Directory.Exists(dir))
{
//////give me an error in here///////
Directory.Delete(dir, true);
}
else
{
MessageBox.Show("folder not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
이것의 목적은 캐시 목적으로 내 메인 프로젝트에 있습니다. 그래서 서버에서 로컬로 복사 한 후 특정 폴더에서 이미지를 얻습니다. 메인 프로젝트를 닫으려면 캐시 나 폴더를 지워야합니다.
최신 정보
더 나은 대안 1 또는 대안 2 (처분)
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
//alternate1
using (FileStream stream = new FileStream(pathx, FileMode.Open, FileAccess.Read))
{
pictureEdit1.Image = Image.FromStream(stream);
//stream.Dispose();
}
//alternate2
//Image img = new Bitmap(pathx);
//pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
//img.Dispose();
}
System.Drawing.Bitmap ( http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx ) 에 대한 설명서 는 다음과 같습니다.
파일은 Bitmap이 삭제 될 때까지 잠겨 있습니다.
이 문제를 해결하려면 다음 줄을 바꿔야합니다.
pictureEdit1.Image = Image.FromFile(pathx);
이것으로 :
Image img = new Bitmap(pathx);
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
img.Dispose();
이렇게하면 PictureBox 컨트롤에서 사용할 이미지의 축소판 버전을 만들 수있을만큼만 비트 맵을로드 한 다음 즉시 삭제하여 파일 잠금을 해제하지만 여전히 화면에 이미지를 표시합니다.
도움이 되었기를 바랍니다!
편집 : 다음 은 동일한 기능 을 사용 하는 사용 버전 입니다.
using (Image img = new Bitmap(pathx)) {
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다