2014-06-24 3 views
-1
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace newconvert 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     Bitmap bitmap; 
     public void Form1_Load(object sender, EventArgs e) 
     { 
      String fileName = 
       @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"; 

      using (Stream bmpStream = 
       System.IO.File.Open(fileName, System.IO.FileMode.Open)) 
      { 
       Image image = Image.FromStream(bmpStream); 

       bitmap = new Bitmap(image); 
       pictureBox1.Image = bitmap; 
      } 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 
     } 

     public void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       String fileName = 
        @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"; 
       String fName = 
        @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"; 
       String Naming = 
        @"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"; 
       String Nam = 
        @"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"; 
       String Namd = 
        @"C:\Users\Public\Pictures\Sample Pictures\Amazingly Funny People Photo #1 (11).jpg"; 

       using (Stream bmpStream = 
        System.IO.File.Open(fileName, System.IO.FileMode.Open)) 
       { 
        bitmap.Dispose(); 
        Image image = Image.FromStream(bmpStream); 

        bitmap = new Bitmap(image); 
        pictureBox2.Image = bitmap; 
        pictureBox2.Height = bitmap.Height; 
        pictureBox2.Width = bitmap.Width;  
       } 

      } 
      catch (Exception a) 
      { 
       MessageBox.Show("" + a); 
      } 
     } 
    } 
} 

Windows 응용 프로그램에서 위 코드를 사용하여 jpg 파일을 비트 맵으로 변환하고 작동되는 이미지를 스트리밍합니다. 하지만 wpf 응용 프로그램에서이 작업을 수행하는 방법을 알아야합니다. 그리드를 사용하여 이미지를 설정하고 있습니다. 그리드에서만 비트 맵 이미지를 사용할 수 있습니다. 어떻게해야합니까? 같은jpg 이미지를 비트 맵 이미지로 변환하는 방법?

+0

가능 중복 된 [I은 C#을 사용, 비트 맵으로 JPG 파일을 변환 할 수있는 방법 ?] (http://stackoverflow.com/questions/24383256/how-can-i-convert-a-jpg-file-into-a-bitmap-using-c) –

+0

Daniel kin dly는 내 질문을 읽었습니다. wpf 응용 프로그램에서 어떻게 할 수 있는지 알아야합니다. – KVK

답변

1

시도 뭔가이

public BitmapImage ImageFromStream(Stream stream) 
{ 
    var image = new BitmapImage(); 
    image.BeginInit(); 
    image.StreamSource = stream; 
    image.EndInit(); 
    return image; 
} 

그리고에서 다음

using (Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open)) 
{ 
    var bitmapImage = ImageFromStream(bmpStream); 
    // etc 
} 
+0

완성을 위해 : EndInit 다음에 즉시 닫히는 스트림에서 BitmapImage를 초기화하는 동안 'BitmapCacheOption.OnLoad' 플래그를 설정해야합니다. 참조를 위해 비고 섹션 [here] (http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.cacheoption.aspx)을 참조하십시오. – Clemens

+0

'image.StreamSource = stream;'을 설정할 때 스트림의 위치를 ​​0으로 설정 했더라도'image.StreamSource = new MemoryStream (stream.ToArray());'를 사용해야하는 경우가 있습니다. – KAI

0

: MSDN

// Open a Stream and decode a JPEG image 
Stream imageStreamSource = new FileStream("tulipfarm.jpg", FileMode.Open, FileAccess.Read, FileShare.Read); 
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
BitmapSource bitmapSource = decoder.Frames[0]; 

// Draw the Image 
Image myImage = new Image(); 
myImage.Source = bitmapSource; 
myImage.Stretch = Stretch.None; 
myImage.Margin = new Thickness(20); 
관련 문제