2016-08-05 4 views
1

내 프로젝트에서 사용자 지정 이미지 파일 (디스크)을 검색해야합니다. 제공된 경로에 이미지 파일이 없으면 기본 이미지 (포함 된 리소스)가 사용됩니다. 이미지가 생기면 이미지 크기를 조정해야 응용 프로그램에서 더 많이 사용할 수 있습니다.리소스 파일의 이미지 개체에 액세스

포함 된 리소스 (코드 섹션 1)에만 액세스하려고하면 예상대로 작동합니다. 이미지 (코드 2 절)에 조건을 배치하는 시도가, 개체가 개체에 대한 예외의 모든 종류의 다시 온다면, 특히 내 목적 : 여기에

((System.Drawing.Image)(ReportLogoToUse)).Height' threw an exception of type 'System.ArgumentException' int {System.ArgumentException} 
((System.Drawing.Image)(ReportLogoToUse)).Width' threw an exception of type 'System.ArgumentException' int {System.ArgumentException} 

내 코드입니다

// Code Section 1 
using (var myImage = Resources.sie_logo_petrol_rgb){ 
    // resize the image to max allowed dimensions (64 x 233) 
    var resizedImage = Helpers.ResizeImage(myImage, (int)maxWidth, (int)maxHeight); // this code executes with no errors 
    pictureBox1.Image = resizedImage; 
} 

// Code Section 2 
using (var ReportLogoToUse = Helpers.ReportLogo(filePath)){ 
    // resize the image to max allowed dimensions (64 x 233) 
    var resizedImage = Helpers.ResizeImage(ReportLogoToUse, (int)maxWidth, (int)maxHeight); // Invalid Parameter error 
    pictureBox2.Image = resizedImage; 
} 

public static Bitmap ReportLogo(string filePath){ 
    try{ 
     var myImage = Image.FromFile(filePath, true); 
     return (Bitmap)myImage; 
    } 
    catch (Exception ex){ 
     // use the embedded logo 
     using (var myResourceImage = Resources.sie_logo_petrol_rgb){ 
      var myImage = myResourceImage; 
      return (Bitmap)myImage; 
     } 
    } 
} 

코드 섹션 1과 코드 섹션 2의 개체 차이점은 무엇입니까? 그들은 같은 종류의 물건을 돌려주지 않습니까?

+0

아마도 프로그램에서 리소스의 이미지 크기를 조정하려고 시도했을 수 있습니다. 새로운 비트 맵 (Helpers.ReportLogo (filePath))과 같은 것이 있는지보십시오. – null

+0

@null : 'Helpers.ResizeImage' 함수를 사용하기 전에 ReportLogoToUse 객체에 문제가 있습니다. – Kulstad

+0

여기에서'return (Bitmap) myImage;'를 디버그하면'try'와'catch' 둘 모두에서 방법? 리소스를 다시 가져와 캐스팅하는 데 문제가 있습니다. 또한'(var myResourceImage ... '를 사용하여 제거하십시오. – null

답변

0

catch... 섹션의 using... 블록을 제거하고 파일 자체 만 반환하면 현재 작동하고있는 것으로 보입니다.

public static Bitmap ReportLogo(string filePath){ 
    try{ 
     return (Bitmap)Image.FromFile(filePath, true); 
    } 
    catch (Exception ex){ 
     // use the embedded logo 
     return Resources.sie_logo_petrol_rgb; 
    } 
} 

왜 현재 작동하는지에 대한 통찰력은 (나는 완전히 당황한 상태이기 때문에) 크게 감사 할 것입니다.