C# 转换函数 (Stream to Byte[],Byte to Image,Image to Byte[],Image缩放,Byte[] to Bitmap)

本文介绍如何实现图片的多种转换操作,包括从Stream到byte[], byte[]到Image, Image到byte[], 以及Image的缩放处理,并提供了将byte[]转换为Bitmap的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Stream转byte[]
  public byte[] StreamToByteArr(Stream stream)
 {
   byte[] bytes = new byte[stream.Length]; //创建byte数组
    stream.Read(bytes, 0, bytes.Length); //将Stream读入byte数组
   stream.Seek(0, SeekOrigin.Begin);// 将流指向起始位置
    return bytes;
 }

2. byte[]转Image
public static Image GetImageByBytes(byte[] bytes)
{
  Image photo = null;
  using (MemoryStream ms = new MemoryStream(bytes))
  {
      ms.Write(bytes, 0, bytes.Length);
     photo = Image.FromStream(ms, true);
   }
    return photo;
}

3.Image转Byte[]
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.Bmp);
      bt = new byte[mostream.Length];
      mostream.Position = 0;
      mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
     }
  }
  return bt;
}

4.Image图片缩放
public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
{
  int width;//图片最终的宽 
  int height;//图片最终的高 
  try
  {
    System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
    Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
    Graphics graphics = Graphics.FromImage(targetPicture);
    graphics .Clear(Color.White);
    //计算缩放图片的大小 
    if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
    {
      width = targetWidth;
      height = (width * sourceImage.Height) / sourceImage.Width;
    }
    else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
    {
      height = targetHeight;
       width = (height * sourceImage.Width) / sourceImage.Height;
    }
     else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
    {
      width = sourceImage.Width;
      height = sourceImage.Height;
    }
    else
    {
      width = targetWidth;
      height = (width * sourceImage.Height) / sourceImage.Width;
      if (height > targetHeight)
      {
        height = targetHeight;
        width = (height * sourceImage.Width) / sourceImage.Height;
       }
    }
     graphics .DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
     sourceImage.Dispose();
     return targetPicture;
  }
  catch (Exception ex)
  {
  }
   return null;
}

5.byte[] 转bitmap
 public static Bitmap BytesToBitmap(byte[] Bytes)
{
    MemoryStream stream = null;
   try
   {
      stream = new MemoryStream(Bytes);
      return new Bitmap((Image)new Bitmap(stream));
    }
    catch (ArgumentNullException ex)
    {
        throw ex;
    }
     catch (ArgumentException ex)
     {
         throw ex;
     }
      finally
     {
         stream.Close();
      }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ant5985

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值