本文介绍了保存并加载writablebitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个writablebitmap写入一个文件,然后从同一个文件加载writablebitmap。


我的图像是正方形(ss x ss resolution)


此代码将writablebitmap写入文件

 StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(" tmp / tmp。 img",FileMode.Create,myStore)); 
BinaryWriter bw = new BinaryWriter(writeFile.BaseStream);
for(int i = 0; i< wb.Pixels.Length; i ++)
{
bw.Write(wb.Pixels [i]);
}
writeFile.Flush();
writeFile.Close();
writeFile.Dispose();

此代码读取相同的文件

 

StreamReader sr = new StreamReader(new IsolatedStorageFileStream(" tmp / tmp.img",FileMode.Open,myStore));
BinaryReader br = new BinaryReader(sr.BaseStream);
WriteableBitmap wb = new WriteableBitmap((int)Math.Sqrt(sr.BaseStream.Length / sizeof(int)),(int)Math.Sqrt(sr.BaseStream.Length / sizeof(int)));

int k = 0;

sr.BaseStream.Seek(0,SeekOrigin.Begin);
while(sr.BaseStream.Position< sr.BaseStream.Length)
{
wb.Pixels [k] = br.ReadInt32();
k ++;
}

  wb.Invalidate();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; sr.Close();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; sr.Dispose();

但阅读速度非常慢。


如何提高代码的速度?


谢谢


解决方案

I have to write a writablebitmap to a file and then load the writablebitmap from the same file.

My image is square (ss x ss resolution)

This code write the writablebitmap to a file

StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("tmp/tmp.img", FileMode.Create, myStore));
            BinaryWriter bw = new BinaryWriter(writeFile.BaseStream);
            for (int i = 0; i < wb.Pixels.Length; i++)
            {
                bw.Write(wb.Pixels[i]);
            }
            writeFile.Flush();
            writeFile.Close();
            writeFile.Dispose();

And this code read the same file

StreamReader sr = new StreamReader(new IsolatedStorageFileStream("tmp/tmp.img", FileMode.Open, myStore)); BinaryReader br = new BinaryReader(sr.BaseStream); WriteableBitmap wb = new WriteableBitmap((int)Math.Sqrt(sr.BaseStream.Length/sizeof(int)), (int)Math.Sqrt(sr.BaseStream.Length/sizeof(int))); int k=0; sr.BaseStream.Seek(0, SeekOrigin.Begin); while(sr.BaseStream.Position<sr.BaseStream.Length) { wb.Pixels[k] = br.ReadInt32(); k++; }

  wb.Invalidate();
                sr.Close();
                sr.Dispose();

But to read is very very slow.

How can I improve the speed of my code?

Thanks

解决方案


这篇关于保存并加载writablebitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:34