繁体   English   中英

在c#中从img src获取图像缩略图

[英]Getting image thumbnail from img src in c#

我将一个字符串传递给我的控制器,其中包含图像 src 字节

前任。

src =“数据:图像/png;base64,sdjsdfojsdf;l”;

在 c# 中,我有一个方法可以从字节 [] 中获取缩略图

public static byte[] CreateImageThumbnail(byte[] image, int width, int height)
{
    using (var stream = new System.IO.MemoryStream(image))
    {
        var img = Image.FromStream(stream);
        var thumbnail = img.GetThumbnailImage(width, height, () => false, IntPtr.Zero);

        using (var thumbStream = new System.IO.MemoryStream())
        {
            thumbnail.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return thumbStream.GetBuffer();
        }
    }
}

但现在我希望能够在上面的示例中传入 src 字符串,然后返回一个新的 src 字符串作为压缩缩略图。

前任。

public static string CreateImageThumbnail(string src, int width, int height)

我需要在 'CreateImageThumbnail' 方法之前和之后进行什么类型的转换,以便在该方法之前和之后传入 src 字符串或将 src 字符串转换为字节数组?

假设输入字符串和输出字符串的格式应该是"data:image/png;base64,<base64encoding>"那么我们可以轻松提取base 64编码,然后转换为字节数组,然后使用您现有的方法压缩它,然后转换回来。

public static string CreateImageThumbnail(string src, int width, int height)
{
    if(src == null) throw new ArgumentNullException(nameof(src));
    string base64Start = "data:image/png;base64,";
    if(!src.StartsWith(base64Start)) throw new ArgumentException("wrong format", nameof(src));

    string base64Input  = src.Substring(base64Start.Length).Trim();
    byte[] imageData    = Convert.FromBase64String(base64Input);
    byte[] outputData   = CreateImageThumbnail(imageData);
    string base64Output = Convert.ToBase64String(outputData);

    return base64Start + base64Output;
}

除此之外, 根据文档的说明,您应该使用MemoryStream.ToArray()方法而不是MemoryStream.GetBuffer()方法:

请注意,缓冲区包含可能未使用的已分配字节。 例如,如果将字符串“test”写入 MemoryStream 对象,则 GetBuffer 返回的缓冲区长度为 256,而不是 4,其中 252 个字节未使用。 要仅获取缓冲区中的数据,请使用 ToArray 方法; 但是,ToArray 在内存中创建数据的副本。

所以你获取字节数组图像数据缩略图的原始代码应该是

public static byte[] CreateImageThumbnail(byte[] image, int width, int height)
{
    using (var stream = new System.IO.MemoryStream(image))
    {
        var img = Image.FromStream(stream);
        var thumbnail = img.GetThumbnailImage(width, height, () => false, IntPtr.Zero);

        using (var thumbStream = new System.IO.MemoryStream())
        {
            thumbnail.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return thumbStream.ToArray();
        }
    }
}

编辑

由于您需要能够使用"data:<mimetype>;base64,<base64encoding>"形式的所有格式,并且 base64 编码的字符串不包含字符 ',',并且也不包含 mime 类型。 所以我们可以使用string.Split()来获取 base64 编码的数据。

public static string CreateImageThumbnail(string src, int width, int height)
{
    if(src == null) throw new ArgumentNullException(nameof(src));
    string[] split = src.Split(',');
    if(split.Length != 2) throw new ArgumentException("wrong format", nameof(src));
    string base64Start  = split[0] + ",";
    string base64Input  = split[1].Trim();
    byte[] imageData    = Convert.FromBase64String(base64Input);
    byte[] outputData   = CreateImageThumbnail(imageData);
    string base64Output = Convert.ToBase64String(outputData);

    return base64Start + base64Output;
}
  using System;
  using System.Text.RegularExpressions;


        // source element
         var ImageLink = "<img src='http://nydomain.com/Image.jpg' />";

        // pattern to extract image source
         string ImageSrc = Regex.Match(ImageLink, "<img.+?src=[\"'](.+?)[\"'].+?>", 
    RegexOptions.IgnoreCase).Groups[1].Value;

        // showing output here
        Console.WriteLine(ImageSrc);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM