zoukankan      html  css  js  c++  java
  • Windows 8各种流之间的转换

    //String 转 Buffer
    private IBuffer GetBufferFromString(String str)
    {
    using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
    {
    using (DataWriter dataWriter = new DataWriter(memoryStream))
    {
    dataWriter.WriteString(str);
    return dataWriter.DetachBuffer();
    }
    }
    }
    
    //Buffer 转 String
    private String GetStringFromBuffer(IBuffer buffer)
    {
    using (DataReader dataReader = DataReader.FromBuffer(buffer))
    {
    return dataReader.ReadString(buffer.Length);
    }
    }
    
    //Stream 转 IInputStream 
    
    IInputStream inputStream = stream.AsInputStream();
    
    //IInputStream 转 Stream
    
    stream  = inputStream.AsStreamForRead();
    
    //Stream 转 IOutputStream 
    
    IOutputStream outputStream = stream.AsOutputStream();
    
    //IOutputStream 转 Stream
    
    stream  = outputStream.AsStreamForRead();
    
    //Stream  转  IRandomAccessStream
    
    IBuffer buffer = null;
    
    var inputstream = stream.AsInputStream();
    
    using (var dataReader = new DataReader(inputstream))
    
    {
    
    await dataReader.LoadAsync((uint)stream.Length);
    
    buffer = dataReader.DetachBuffer();
    
    }
    
    var iRandomAccessStream = new InMemoryRandomAccessStream();
    
    await iRandomAccessStream.WriteAsync(buffer);
    
    // IRandomAccessStream 转 Stream
    
    stream = iRandomAccessStream.AsStream();  
    
    //Ibuffer转byte[]
    
    byte[] bytes = WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);
    
    //Byte[]转Ibuffer
    
    IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length);
    
    //Ibuffer转Stream
    
    Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);
    
    //Stream转Ibuffer
    
    MemoryStream memoryStream = new MemoryStream();  
    
    if (stream != null)
    
    {
    
    byte[] bytes = ReadFully(stream);
    
    if (bytes != null)
    
    {
    
    var binaryWriter = new BinaryWriter(memoryStream);
    
    binaryWriter.Write(bytes);
    
    }
    
    } 
    
    IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);
    
    //Ibuffer转byte[]
    
    byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);
    
    //Byte[]转Ibuffer
    
    WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length);
    
    //Ibuffer转IrandomAccessStream
    
    InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream();
    
    DataWriter datawriter = new DataWriter(inStream.GetOutputStreamAt(0));
    
    datawriter.WriteBuffer(buffer,0,buffer.Length);
    
    await datawriter.StoreAsync();
    
    //IrandomAccessStream转Ibuffer
    
    Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
    
    MemoryStream memoryStream = new MemoryStream();  
    
    if (stream != null)
    
    {
    
    byte[] bytes = await ConvertStreamTobyte(stream);
    
    if (bytes != null)
    
    {
    
    var binaryWriter = new BinaryWriter(memoryStream);
    
    binaryWriter.Write(bytes);
    
    }
    
    } 
    
    IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);
    
    //Stream转byte[]
    
    public async Task<byte[]> ConvertStreamTobyte(Stream stream)
    
    {
    
    MemoryStream ms = new MemoryStream();
    
    await stream.CopyToAsync(ms);
    
    return ms.ToArray();
    
    }
    
    //Byte转Stream
    
    public Stream BytesToStream(byte[] bytes)
    
    {
    
    Stream stream = new MemoryStream(bytes);
    
    return stream;
    
    }
    
    //Byte[] -> BitmapImage
    
    public async Task<BitmapImage> ByteArrayToImageAsync(byte[] pixeByte)
    
    {
    
    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
    
    {
    
    stream.Seek(0);
    
    IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(pixeByte, 0, pixeByte.Length);
    
    await stream.WriteAsync(buffer);
    
    BitmapImage image = new BitmapImage();
    
    image.SetSource(stream);
    
    return image;
    
    }
    
    }
  • 相关阅读:
    Git与GitHub的基本使用
    HTML&CSS基础-外边框
    HTML&CSS基础-内边框
    SHELL脚本编程变量输入
    GoLang基础数据类型-切片(slice)详解
    GoLang基础数据类型--->数组(array)详解
    SHELL脚本编程的运算符
    SHELL脚本编程变量详解
    HTML&CSS基础-边框简写属性
    GoLang基础数据类型--->字符串处理大全
  • 原文地址:https://www.cnblogs.com/wzwyc/p/6293417.html
Copyright © 2011-2022 走看看