字节和流为图片和此流不支持操作解决

图片读取

//路径  
Image img = Image.FromFile(path);
//文件流
Image img = Image.FromStream(ms)
//Bitmap也一样 ,只是
Bitmap bit = new Bitmap(img);

图片转字节

public byte[] ImageToBytes(Image img)
 {
      using (MemoryStream ms = new MemoryStream())
      {
          Bitmap bit = new Bitmap(img);
          bit.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
          return ms.ToArray();
       }
 }

字节转图片

public Bitmap BytesToImage(byte[] buffer)
{
      MemoryStream ms = new MemoryStream(buffer);
      Image image = System.Drawing.Image.FromStream(ms);
      return new Bitmap(image);
}

都是 字节=》流=》图片

图片转换为黑白

 public Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                }
            }
            return bm;
        }

图片压缩

 #region getThumImage
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="sourceFile">原始图片文件</param>
        /// <param name="imageQuality">质量压缩比</param>
        /// <param name="multiple">收缩倍数</param>
        /// <param name="outputFile">输出文件名</param>
        /// <returns>成功返回true,失败则返回false</returns>
        public byte[] getThumImage(Bitmap sourceImage, long imageQuality, int multiple)
        {
                ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
                myEncoderParameters.Param[0] = myEncoderParameter;
                float xWidth = sourceImage.Width;
                float yWidth = sourceImage.Height;
                Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple));
                Graphics g = Graphics.FromImage(newImage);
                g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple);
                g.Dispose();
                using (MemoryStream ms = new MemoryStream())
                {
                    newImage.Save(ms, myImageCodecInfo, myEncoderParameters);
                    myEncoderParameters.Dispose();
                    myEncoderParameter.Dispose();
                    return ms.ToArray();
                }
        }


        /// <summary>
        /// 获取图片编码信息
        /// </summary>
        private ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
        #endregion

文件流

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
MemoryStream ms = new MemoryStream(buffer);

 此流不支持操作,将得到的流转为内存流

private byte[] FileToByte(string httpPath)
        {
            Stream sm = WebRequest.Create(httpPath).GetResponse().GetResponseStream();
            MemoryStream ms = new MemoryStream();
            byte[] buffer = new byte[1024];
            while (true)
            {
                int sz = sm.Read(buffer, 0, 1024);
                if (sz == 0) break;
                ms.Write(buffer, 0, sz);
            }
            ms.Position = 0;
            byte[] byData = ms.ToArray();
            sm.Close();
            ms.Close();
            return byData;
        }

 

posted @ 2019-01-29 15:43  世人皆萌  阅读(511)  评论(0编辑  收藏  举报