Stream/Bytes[]/Image对象相互转化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    // 设置当前流的位置为流的开始
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}
 
 
 
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}
 
 
 
// <summary>
/// 字节流转换成图片
/// </summary>
/// <param name="byt">要转换的字节流</param>
/// <returns>转换得到的Image对象</returns>
public static Image BytToImg(byte[] byt)
{
    try
    {
        MemoryStream ms = new MemoryStream(byt);
        Image img = Image.FromStream(ms);
        return img;
    }
    catch (Exception ex)
    {
        LogHelper.WriteError("StreamHelper.BytToImg 异常", ex);
        return null;
    }
}
 
 
 
/// <summary>
///  图片转换成字节流
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public static byte[] ImageToByteArray(Image img)
{
    ImageConverter imgconv = new ImageConverter();
    byte[] b = (byte[])imgconv.ConvertTo(img, typeof(byte[]));
    return b;
}
 
 
 
/// <summary>
/// 把图片Url转化成Image对象
/// </summary>
/// <param name="imageUrl"></param>
/// <returns></returns>
public static Image Url2Img(string imageUrl)
{
    try
    {
        if (string.IsNullOrEmpty(imageUrl))
        {
            return null;
        }
 
        WebRequest webreq = WebRequest.Create(imageUrl);
        WebResponse webres = webreq.GetResponse();
        Stream stream = webres.GetResponseStream();
        Image image;
        image = Image.FromStream(stream);
        stream.Close();
 
        return image;
    }
    catch (Exception ex)
    {
        LogHelper.WriteError("StreamHelper.Url2Img 异常", ex);
    }
 
    return null;
}
 
 
 
/// <summary>
/// 把本地图片路径转成Image对象
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
public static Image ImagePath2Img(string imagePath)
{
    try
    {
        if (string.IsNullOrEmpty(imagePath))
        {
            return null;
        }
 
        byte[] bytes = Image2ByteWithPath(imagePath);
        Image image = BytToImg(bytes);
        return image;
    }
    catch (Exception ex)
    {
        LogHelper.WriteError("StreamHelper.ImagePath2Img 异常", ex);
        return null;
    }
}

  

posted @   深南大道  阅读(499)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示