我刚刚开始学习wpf绑定。所以我非常菜鸟
我尝试将图像上传到memorystream中,进行旋转,然后再从memorystream中读取它,然后像绑定?!?一样加载到image.source中。
我找到了一些代码,尝试将它们放在一起但没有成功 我不完全知道我需要成功
mainWindow
<Window.Resources>
<local:Imgcls x:Key="imgcls" />
<!--<local:ByteToImageConverter x:Name="binaryConverter" x:Key="byteToImageConverter"/>-->
</Window.Resources>
<Grid>
<DockPanel Height="28" VerticalAlignment="Top">
</DockPanel>
<DockPanel Height="360">
<Grid Width="400" Margin="10">
<StackPanel>
<Button Content="load" Height="50" Click="Btn_Load_Click"/>
<Button Content="open" Height="50" Click="Btn_Open_Click"/>
<Button Content="rotate" Height="50" Click="Btn_Rotate_Click"/>
</StackPanel>
</Grid>
<Grid Width="360">
<Border BorderBrush="Black" BorderThickness="1">
<Image x:Name="img_box" Margin="5,5,5,5" Stretch="Uniform"
Source="{Binding UriImg ,Source={StaticResource imgcls}}" />
</Border>
</Grid>
</DockPanel>
<DockPanel Height="28" VerticalAlignment="Bottom">
</DockPanel>
</Grid>
Imgcls.cs
public class Imgcls : INotifyPropertyChanged
{
public Imgcls()
{
UriImg = new Uri("pack://application:,,,/img/pic.png", UriKind.Absolute);
}
private Uri uri_img;
public Uri UriImg
{
get {return uri_img;}
set { uri_img = value; }
}
private byte[] byteimg;
public byte[] ByteImg
{
get { return byteimg; }
set { byteimg = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void Notify(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
}
public class ByteToImageConverter : IValueConverter
{
public BitmapImage ConvertImgToByte(Uri source)
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = source;
img.CacheOption = BitmapCacheOption.OnLoad;
//img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
img.EndInit();
return img;
}
public BitmapImage ConvertByteToImg(byte[] imgByteArray)
{
MemoryStream stream = new MemoryStream(imgByteArray);
BitmapImage img = new BitmapImage();
img.BeginInit();
img.StreamSource = stream;
img.EndInit();
return img;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage img = new BitmapImage();
if (value != null)
{
img = ConvertImgToByte(value as Uri);
}
return img;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage img = new BitmapImage();
if (value != null)
{
img = ConvertByteToImg(value as byte[]);
}
return img;
}
}
mainwindow.xaml.cs
//private double rotation = 0;
//BitmapImage source = new BitmapImage(new Uri(Uri_img));
public MainWindow()
{
InitializeComponent();
}
private void Btn_Load_Click(object sender, RoutedEventArgs e)
{
ByteToImageConverter bi = new ByteToImageConverter();
bi.ConvertImgToByte(new Uri(@"d:\Picture\images.png"));
}
private void Btn_Open_Click(object sender, RoutedEventArgs e)
{
ByteToImageConverter bi = new ByteToImageConverter();
//bi.ConvertByteToImg(Imgbyte);
}
private void Btn_Rotate_Click(object sender, RoutedEventArgs e)
{
//rotation += 90;
//img_box.Source = new TransformedBitmap(source, new RotateTransform(rotation));
}
我尝试旋转内存流中的图像,反转等,并从内存流中绑定img_box.source