C# 将图像文件打开为可写位图
问题就在这里。 我想从本地驱动器打开一个文件,然后将其转换成可写位图,以便编辑它。 但问题是,我无法从Uri或类似的东西创建可写位图。我还知道如何将文件打开到BitmapImage,但我不知道如何将文件打开为可写位图。 是否有办法将文件直接打开为可写位图,如果没有,是否有办法将位图图像转换为可写位图?C# 将图像文件打开为可写位图,c#,silverlight,writeablebitmap,C#,Silverlight,Writeablebitmap,问题就在这里。 我想从本地驱动器打开一个文件,然后将其转换成可写位图,以便编辑它。 但问题是,我无法从Uri或类似的东西创建可写位图。我还知道如何将文件打开到BitmapImage,但我不知道如何将文件打开为可写位图。 是否有办法将文件直接打开为可写位图,如果没有,是否有办法将位图图像转换为可写位图? 谢谢大家。我不是专家,也不能立即访问intellisense等等,但接下来 var fileBytes = File.ReadAllBytes(fileName); var stream = ne
谢谢大家。我不是专家,也不能立即访问intellisense等等,但接下来
var fileBytes = File.ReadAllBytes(fileName);
var stream = new MemoryStream(fileBytes);
var bitmap = new BitmapImage(stream);
var writeableBitmap = new WritableBitmap(bitmap);
即使不是一个完美的例子,这也足以为你指明正确的方向。希望如此。您可以将图像文件加载到中,并将其用作以下内容的源:
在Silverlight 5中,您可以使用以下方法从本地磁盘打开文件,并将其转换为BitmapImage,然后将其转换为WriteableBitmap
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.ShowDialog();
byte[] byteArray = new byte[] { };
if (dlg.File == null) return;
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
// bi.UriSource = new Uri(@"C:\Users\saw\Desktop\image.jpg", UriKind.RelativeOrAbsolute);
bi.SetSource(dlg.File.OpenRead());
WriteableBitmap eb=new WriteableBitmap(bi);
在尝试创建WriteableBitmap时,设置新Uri时出错(对象引用未设置为对象的实例)无法将位图直接传递到WriteableBitmap
文件中。ReadAllBytes(文件名)在Silverlight中不允许使用代码>,除非在OpenFileDialog中,我们似乎可以将位图直接传递到writablebitmaps构造函数中。我的VS在我第一次尝试时给了我错误,但现在它似乎起作用了。这根本不是一个好问题,抱歉,伙计们。它会工作的,但如果超过百万像素,它会被调整大小。实际上,在Siverlight中,它给了我一个例外。所以它不是一个solution@Peter,您会收到哪个异常?BitmapImage bi=new BitmapImage(新Uri(“header.png”,UriKind.Relative));WriteableBitmap wb=新的WriteableBitmap(bi)
System.NullReferenceException第二行未经用户代码处理:对象引用未设置为对象的实例。
@Peter,在调用BitmapImage
构造函数后,您是否检查了bi
是否为null
?如果是的话,WriteableBitmap
构造函数失败是合乎逻辑的。是的,我检查过了。看看我的答案。你介意在你的机器上测试我的代码吗?
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.ShowDialog();
byte[] byteArray = new byte[] { };
if (dlg.File == null) return;
BitmapImage bi = new BitmapImage();
bi.CreateOptions = BitmapCreateOptions.None;
// bi.UriSource = new Uri(@"C:\Users\saw\Desktop\image.jpg", UriKind.RelativeOrAbsolute);
bi.SetSource(dlg.File.OpenRead());
WriteableBitmap eb=new WriteableBitmap(bi);