• C#图片转成流,流转成图片,字节转图片,图片转字节的方法


    图片转成流

    Bitmap b = new Bitmap(Server.MapPath(ppath));
    Stream ms = new MemoryStream();
    b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

    流转成图片

    MemoryStream ms = new MemoryStream(Bytes);
    return Bitmap.FromStream(ms,true);

    字节转图片

    /// <summary>
    /// 字节数组生成图片
    /// </summary>
    /// <param name="Bytes">字节数组</param>
    /// <returns>图片</returns>
    private Image byteArrayToImage(byte[] Bytes)
    {
        MemoryStream ms = new MemoryStream(Bytes);
        return Bitmap.FromStream(ms,true);
    }

    图片转字节

    /// <summary>
    /// 根据图片路径返回图片的字节流byte[]
    /// </summary>
    /// <param name="imagePath">图片路径</param>
    /// <returns>返回的字节流</returns>
    privatestaticbyte[] getImageByte(stringimagePath)
    {
    FileStream files = newFileStream(imagePath, FileMode.Open);
    byte[] imgByte = newbyte[files.Length];
    files.Read(imgByte, 0, imgByte.Length);
    files.Close();
    returnimgByte;
    }

     剪切图片与将剪切的图片生成内存流MemoryStream

    int pw = 60;
    int ph = 15;
    int px =80;
    int py = 100; 
    ppath = @"d:123.jpg";
    Bitmap b = new Bitmap(ppath);
    //剪裁图片
    RectangleF rec = new RectangleF(px, py, pw, ph);
    Bitmap nb = b.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    //剪切后并读入到流中去
    Stream ms = new MemoryStream();
    nb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  • 相关阅读:
    Codeforces 449D:Jzzhu and Numbers
    51nod 1040:最大公约数之和
    51nod 1179:最大的最大公约数
    51nod 1406:与查询
    51nod 1354:选数字
    51nod 1616:最小集合
    Codeforces:Colored Balls
    素性测试
    秒转换成年月日时分秒 和复制文本到剪贴板
    vue项目中获取cdn域名插件
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7198900.html
Copyright © 2020-2023  润新知