我是靠谱客的博主 成就电脑,最近开发中收集的这篇文章主要介绍C#网络图片处理类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C# 从网络抓取图片处理类,涉及到图片 和 Base64的互相转换以及Base64 转 图片的保存,方便后期查漏补缺。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
/// <summary>
/// 网络图片服务类
/// </summary>
internal class WebImageHelper
{
/// <summary>
/// 下载Web网页的图片,并转换为Base64String格式
/// </summary>
/// <param name="urlAddress">图片URL地址</param>
/// <returns></returns>
public string WebImageToBase64(string urlAddress)
{
try
{
return ImageToBase64(urlAddress);
}
catch (Exception ex)
{
return string.Empty;
}
}
/// <summary>
/// Image 转成 base64
/// </summary>
/// <param name="fileFullName"></param>
private string ImageToBase64(string fileFullName)
{
try
{
Image img = UrlToImage(fileFullName);
Bitmap bmp = new Bitmap(img);
MemoryStream ms = new MemoryStream();
var suffix = fileFullName.Substring(fileFullName.LastIndexOf('.') + 1,
fileFullName.Length - fileFullName.LastIndexOf('.') - 1).ToLower();
var suffixName = suffix == "png"
? ImageFormat.Png
: suffix == "jpg" || suffix == "jpeg"
? ImageFormat.Jpeg
: suffix == "bmp"
? ImageFormat.Bmp
: suffix == "gif"
? ImageFormat.Gif
: ImageFormat.Jpeg;
bmp.Save(ms, suffixName);
byte[] arr = new byte[ms.Length]; ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length); ms.Close();
string base64Str = Convert.ToBase64String(arr);
return PrefectBase64(base64Str);
}
catch (Exception ex)
{
return null;
}
}
private static Image UrlToImage(string url)
{
WebClient mywebclient = new WebClient();
byte[] Bytes = mywebclient.DownloadData(url);
using (MemoryStream ms = new MemoryStream(Bytes))
{
Image outputImg = Image.FromStream(ms);
return outputImg;
}
}
// 处理base64字符
private static string PrefectBase64(string Base64Str)
{
string dummyData = Base64Str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("_", "/").Replace("-", "+");
if (dummyData.Length % 4 > 0)
{
dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
}
return dummyData;
}
#region 保存base64格式图片
public static void SavePhotoByString(string base64, string fullPath)
{
byte[] buffer = Convert.FromBase64String(base64.Replace(" ", "+"));        //保存到指定路径
FileStream fileStream = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
{
Position = 0L
};
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
}
#endregion
/// <summary>
/// 文件转base64编码
/// </summary>
/// <param name="FileName">文件路径+文件名</param>
/// <returns>Base64字符串</returns>
public string FileJpgConvertToBase64(string FileName)
{
if (!File.Exists(FileName))
{
return "";
}
string Pub_Zp = "";
try
{
FileStream fs = File.OpenRead(FileName);
int i = (int)fs.Length;
byte[] Zp = new byte[i];
fs.Read(Zp, 0, i);
fs.Close();
Pub_Zp = Convert.ToBase64String(Zp);
}
catch (Exception ex)
{
Console.WriteLine("JpgConvertToBase64", FileName, ex);
return "";
}
return Pub_Zp;
}
#region[base64转图片]
/// <summary>
/// 解析照片
/// </summary>
/// <param name="base64string">照片base64String类型</param>
/// <returns></returns>
private Bitmap GetImageFromBase64(string base64string)
{
Bitmap bitmap = null;
try
{
if (!string.IsNullOrEmpty(base64string))
{
byte[] b = Convert.FromBase64String(base64string);
MemoryStream ms = new MemoryStream(b);
bitmap = new Bitmap(ms);
}
}
catch (Exception ex)
{
Console.WriteLine("GetImageFromBase64", ex);
}
return bitmap;
}
#endregion
}
}

最后

以上就是成就电脑为你收集整理的C#网络图片处理类的全部内容,希望文章能够帮你解决C#网络图片处理类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部