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应用程序中执行此操作。我使用网格来设置图像。在网格中只有bitmapimage可用。我怎么能这样做?
答案 0 :(得分: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
}
答案 1 :(得分: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);