2011-08-20 5 views
0

WinForms의 PictureBox에서 이미지를로드하는 것과 관련하여 약간의 의문점이 있습니다.
내 양식의 PictureBox에 파일 시스템의 이미지 파일을 표시하려고합니다 (예 : form1).WinForm의 PictureBox에서 파일 시스템의 이미지를 표시하는 방법

저는 C#을 사용하여 Windows 응용 프로그램을 만들고 있습니다.

pdf/text/png/gif/jpeg라고도하는 파일 형식을 확인하고 싶습니다.
C#을 사용하여 프로그래밍 방식으로 파일 시스템에서 파일을 열 수 있습니까?
누구든지 알고 있다면이를 수행하기위한 아이디어 나 샘플 코드를 제공해주세요.

수정 된 코드 : 내 시스템에서 파일을 여는 데 이렇게했는데 파일을 첨부하고 첨부하는 방법을 모르겠습니다.

private void button1_Click(object sender, EventArgs e) 
{  
     string filepath = @"D:\"; 

    openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|"; 
    openFileDialog1.CheckFileExists = true; 
    openFileDialog1.CheckPathExists = true; 

    if (openFileDialog1.ShowDialog(this) == DialogResult.OK) 
    { 
     try 
     { 


     }   
    } 
} 

나는 try 블록에 쓸 필요가 무엇인지 모른다. 누구든지이 일을 도울 수 있습니까?

+0

무엇에 업로드 하시겠습니까? 웹 페이지? 서비스? 파일 서버? 이메일? – Tridus

+0

내 시스템에서 이미지를 업로드하고 싶고 asp.net 웹 응용 프로그램의 파일 업로드 컨트롤처럼 .. 내 양식 상자에 업로드 된 이미지를 표시하고 싶습니다 .. 버튼 하나를 클릭하면 파일이 열립니다 내 시스템에서 ... 이런 식으로 ... – user903550

+0

어떤 샘플 코드라도 조금이라도 도움이된다면 .... – user903550

답변

2

사용 Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx 방법

이미지 IMG = Image.ImageFromFile (openFileDialog1.FileName);

작동해야합니다.

편집

당신의 PictureBox로 설정하는 것, 그리고 무엇 내부 전체 볼 수있는 경우는, PictureBox를

SizeMode 속성을 사용합니다.

+0

thanq tigran 그 작업이 지금은 가능하지만 전체 이미지를 볼 수 없어 이미지를 압축하고 싶습니다. .. – user903550

+0

어디에 표시할까요? – Tigran

+0

은 form1에 설정되었습니다 ... – user903550

0
 using System.IO; 

     openFileDialog1.FilterIndex = 1; 
     openFileDialog1.Multiselect = false;  //not allow multiline selection at the file selection level 
     openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog 
     openFileDialog1.InitialDirectory = @"Desktop"; //define the initial directory 


     if (openFileDialog1.ShowDialog(this) == DialogResult.OK) 
     { 
      try 
      { 
       string filename = openFileDialog1.FileName; 
       FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream 
       Byte[] bindata=new byte[Convert.ToInt32(fs.Length)]; 
       fs.Read(bindata, 0, Convert.ToInt32(fs.Length)); 
       MemoryStream stream = new MemoryStream(bindata);//load picture 
       stream.Position = 0; 
       pictureBox1.Image = Image.FromStream(stream); 
      }   
     } 
관련 문제