zoukankan      html  css  js  c++  java
  • C# 图片转二进制

    /// <summary>
    /// 将目标路径转为image
    /// </summary>
    /// <param name="path">图片路径</param>
    /// <returns></returns>
    public Image LoadImage(string path)
    {
        //创建一个bitmap类型的bmp变量来读取文件。
        Bitmap bmp = new Bitmap(path);
        //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
        Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format16bppRgb555);
        //将第一个bmp拷贝到bmp2中
        Graphics draw = Graphics.FromImage(bmp2);
        draw.DrawImage(bmp, 0, 0);
    
        draw.Dispose();
        bmp.Dispose();//释放bmp文件资源
    
        return (Image)bmp2;
    }
    
    /// <summary>
    /// 将图片转为二进制
    /// </summary>
    /// <param name="img">图片</param>
    /// <returns></returns>
    public byte[] GetByteImage(Image img)
    {
    
        byte[] bt = null;
        if (!img.Equals(null))
        {
            using (MemoryStream mostream = new MemoryStream())
            {
                Bitmap bmp = new Bitmap(img);
    
                bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流
    
                bt = new byte[mostream.Length];
    
                mostream.Position = 0;//设置留的初始位置
    
                mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
    
            }
        }
        return bt;
    }
  • 相关阅读:
    Linux命令——getfacl、setfacl
    Linux命令——groups
    Linux命令——gdisk、fdisk、partprobe
    Linux命令——parted
    Linux命令——blkid
    Linux命令——chattr、lsattr
    Linux命令——od
    Linux命令——basename、dirname
    Linux命令——chgrp、chown、chmod
    Linux命令——pidof
  • 原文地址:https://www.cnblogs.com/Mzg121584668/p/7826586.html
Copyright © 2011-2022 走看看