C# 读取文件展示图片

读取文件显示图片

using (FileStream fs = new FileStream(f.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    MemoryStream ms = new MemoryStream(buffer);
    BitmapImage img = new BitmapImage();
    img.BeginInit();
    img.StreamSource = ms;
    img.CacheOption = BitmapCacheOption.OnLoad;
    img.EndInit();
    ms.Dispose();

    //或
    FileImage.Source = img;
    //或
    BitmapImage img1 = new BitmapImage();
    img1.BeginInit();
    img1.CacheOption = BitmapCacheOption.OnLoad;
    img1.UriSource = new Uri("path", UriKind.RelativeOrAbsolute);
    img1.Freeze();
    img1.EndInit();
}

他人的:https://blog.csdn.net/qq_18995513/article/details/53693554

    Bitmap和BitmapImage相互转换。
    RenderTargetBitmap –> BitmapImage
    ImageSource –> Bitmap
    BitmapImage和byte[]相互转换。
    byte[]> Bitmap
// Bitmap --> BitmapImage
public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
    using (MemoryStream stream = new MemoryStream())
    {
        bitmap.Save(stream, ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度

        stream.Position = 0;
        BitmapImage result = new BitmapImage();
        result.BeginInit();
        // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
        // Force the bitmap to load right now so we can dispose the stream.
        result.CacheOption = BitmapCacheOption.OnLoad;
        result.StreamSource = stream;
        result.EndInit();
        result.Freeze();
        return result;
    }
}


// BitmapImage --> Bitmap
public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
    // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));

    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        Bitmap bitmap = new Bitmap(outStream);

        return new Bitmap(bitmap);
    }
}

// RenderTargetBitmap --> BitmapImage
public static BitmapImage ConvertRenderTargetBitmapToBitmapImage(RenderTargetBitmap wbm)
{
    BitmapImage bmp = new BitmapImage();
    using (MemoryStream stream = new MemoryStream())
    {
        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(wbm));
        encoder.Save(stream);
        bmp.BeginInit();
        bmp.CacheOption = BitmapCacheOption.OnLoad;
        bmp.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        bmp.StreamSource = new MemoryStream(stream.ToArray()); //stream;
        bmp.EndInit();
        bmp.Freeze();
    }
    return bmp;
}


// RenderTargetBitmap --> BitmapImage
public static BitmapImage RenderTargetBitmapToBitmapImage(RenderTargetBitmap rtb)
{
    var renderTargetBitmap = rtb;
    var bitmapImage = new BitmapImage();
    var bitmapEncoder = new PngBitmapEncoder();
    bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

    using (var stream = new MemoryStream())
    {
        bitmapEncoder.Save(stream);
        stream.Seek(0, SeekOrigin.Begin);

        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = stream;
        bitmapImage.EndInit();
    }

    return bitmapImage;
}

// ImageSource --> Bitmap
public static System.Drawing.Bitmap ImageSourceToBitmap(ImageSource imageSource)
{
    BitmapSource m = (BitmapSource)imageSource;

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // 坑点:选Format32bppRgb将不带透明度

    System.Drawing.Imaging.BitmapData data = bmp.LockBits(
    new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

    m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
    bmp.UnlockBits(data);

    return bmp;
}

// BitmapImage --> byte[]
public static byte[] BitmapImageToByteArray(BitmapImage bmp)
{
    byte[] bytearray = null;
    try
    {
        Stream smarket = bmp.StreamSource; ;
        if (smarket != null && smarket.Length > 0)
        {
            //设置当前位置
            smarket.Position = 0;
            using (BinaryReader br = new BinaryReader(smarket))
            {
                bytearray = br.ReadBytes((int)smarket.Length);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    return bytearray;
}


// byte[] --> BitmapImage
public static BitmapImage ByteArrayToBitmapImage(byte[] array)
{
    using (var ms = new System.IO.MemoryStream(array))
    {
        var image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad; // here
        image.StreamSource = ms;
        image.EndInit();
        image.Freeze();
        return image;
    }
}
//byte>bitmap
public static System.Drawing.Bitmap ConvertByteArrayToBitmap(byte[] bytes)
{
    System.Drawing.Bitmap img = null;
    try
    {
        if (bytes != null && bytes.Length != 0)
        {
            MemoryStream ms = new MemoryStream(bytes);
            img = new System.Drawing.Bitmap(ms);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    return img;
}


你可能感兴趣的:(C#,c#)