'Stream'에 해당되는 글 2건
- 2008/01/06 [C#, .Net] PictureBox 에 URL 이미지 주소 사용하기
- 2007/11/05 C# - OpenFileDialog 사용, Binary 데이터 읽기 코드
위와 같이 iis 쪽에 해당 이미지가 있다고 했을때 Winform 쪽에 PictureBox에 이미지를 Url 로 추가 하면 안되는거 같다.( 바로 적용이 가능한지 아시는분은 답변 부탁 ^^)
그래서 웹 브라우져 컨트롤을 써자니 좀 이상해서 PictureBox에 사용하기 위해서는 원격 주소에 있는 데이터를 클라이언트로 가져와야 하는걸 알았다.
이때 두가지 경우가 있는데 스트림, 파일 이 있다. 파일 보다는 스트림이 나을거 같아서 이것을 채택 했다.
그리고 위의 소스를 보면 아시겠지만 DB의 정보를 가져 와서 보여 주는 뷰어로 사용할려다 보니 DataSet와 연동을 하였다.
icon_img 정보를 가져 와서 iis의 주소에 맵핑 해서 해당 이미지를 보여 주도록 하였다.
그래서 바인딩 부분에서 몇가지 수정이 필요하다.
PictureBox에 바인딩이 있다고 가정한다.
Binding binding = icon_imgPictureBox.DataBindings["Image"];
binding.Format += new ConvertEventHandler(binding_Format);
위와 같이 Format 이벤트를 하나 생성한다.
그리고 아래와 같이 작성합니다.
void binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value != null)
{
try
{
//e.Value에 있는 값을 사용해서 파일 경로를 생성한다.
string filepath = "http://local:8888/Icon/2000/" + e.Value + ".gif";
// WebClient 객체를 생성해서 스트림을 가져 오도록 한다.
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(filepath);
// Stream 값을 가져 온다.
Stream stream = new MemoryStream();
stream.Write(myDataBuffer, 0, myDataBuffer.Length);
// PictureBox에 스트림으로 로드 한다.
this.icon_imgPictureBox.Image = Image.FromStream(stream, true);
}
catch (WebException webex)
{
}
}
}
try, catch 를 사용한 이유는 e.Value 에 값이 파일 이름이 아닌 다른 정보가 들어 오는 경우가 있다. 그래서 catch로 넘기도록 해서 에러를 없애도록 한다.
Openfiledialog를 사용해서 파일을 가져와서 Stream의 정보를 Binary로 읽는 코드 입니다.
private void btnAzeSelect_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// 컨트롤에 이름 설정하기
tbAzeName.Text = openFileDialog1.FileName;
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// 바이너리로 읽기 위해서 BinaryReader를 생성하고 myStream의
// 스트림 값을 넘겨 받는다.
BinaryReader r = new BinaryReader(myStream);
for (int i = 0; i < 5; i++)
{
Console.WriteLine(r.ReadInt32());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}

Prev
Rss Feed