Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何处理嵌入在电子邮件中的图像并调整大小_Image Processing_Xamarin.forms_Mime Mail - Fatal编程技术网

Image processing 如何处理嵌入在电子邮件中的图像并调整大小

Image processing 如何处理嵌入在电子邮件中的图像并调整大小,image-processing,xamarin.forms,mime-mail,Image Processing,Xamarin.forms,Mime Mail,我们的代码在Xamarin(和.Net标准2.0)之外运行良好,但在尝试在Xamarin Forms应用程序中执行时出现平台不支持错误(在Image.FromStream(…)行)。此代码处理保存在数据库中的电子邮件,并尝试调整图像大小,然后重新输出邮件正文以供Xamarin WebView控件使用。如果Image.FromStream()不适用于我们,我们如何解决这个问题 下面是进行处理的代码(可以假设比率为50作为示例): // ///将电子邮件中的图像转换为Base64嵌入源 /// /

我们的代码在Xamarin(和.Net标准2.0)之外运行良好,但在尝试在Xamarin Forms应用程序中执行时出现平台不支持错误(在Image.FromStream(…)行)。此代码处理保存在数据库中的电子邮件,并尝试调整图像大小,然后重新输出邮件正文以供Xamarin WebView控件使用。如果Image.FromStream()不适用于我们,我们如何解决这个问题

下面是进行处理的代码(可以假设比率为50作为示例):

//
///将电子邮件中的图像转换为Base64嵌入源
/// 
///表示已加载电子邮件的mimessage
///该消息的Body属性
///新的图像比例
///用于UI显示的新正文的字符串
私有静态字符串PopulateInlineImages(mimessage newMessage,字符串bodyHtml,double-imageResizeRatio=1)
{
//编码为base64以便于下载/存储
如果(bodyHtml!=null)
{
foreach(newMessage.BodyParts中的MimePart att)
{
if(att.ContentId!=null&&att.Content!=null&&att.ContentType.MediaType==“image”&&&(bodyHtml.IndexOf(“cid:+att.ContentId,StringComparison.Ordinal”)>-1))
{
字节[]b;
使用(var mem=new MemoryStream())
{
附件内容解码(mem);
b=还原(mem、imageResizeRatio);
//b=内存ToArray();
}
string imageBase64=“数据:”+att.ContentType.MimeType+“base64”+System.Convert.tobase64字符串(b);
bodyHtml=bodyHtml.Replace(“cid:+att.ContentId,imageBase64”);
}
}
}
返回bodyHtml;
}
私有静态字节[]ReduceSize(Stream-Stream,double-imageResizeRatio=1)
{
图像源=Image.FromStream(流);
图像缩略图=source.GetThumbnailImage((int)(source.Width*imagerresizeratio),(int)(source.Height*imagerresizeratio),AbortCallback,IntPtr.Zero);
使用(var memory=new MemoryStream())
{
缩略图.保存(内存,源.RawFormat);
返回内存。ToArray();
}
}
私有静态bool AbortCallback()
{
返回false;
}

将SkiaSharp用于跨平台图像/绘图工具我已经做了一次徒劳无功的搜索,试图将该库用于我的情况。事实上,我在Microsoft()中找到的一个链接指出“警告此API现在已过时”。您是否有任何网站/资源可以提供从流创建图像、调整图像大小并允许将其输出到SkiaSharp中的字节数组的可靠示例?如果您查看详细信息,它已被ScalePixels()所取代-我很感谢你的快速回复,杰森。不幸的是,我仍然被卡住了。当我尝试“SKBitmap bm=SKBitmap.Decode(新的SKManagedStream(stream));”时,我得到一个空值(表示一个错误),但是流很好…解码失败。另外,我意识到StackOverflow会更喜欢将此移动到聊天…但我不知道如何做到这一点(DOH!)。一旦我拿到了它,我想我会是金色的!您确定您的流包含有效的图像数据吗?你有没有试过直接解码
——我认为没有必要使用SKManagedStream。
        /// <summary>
        /// Converts images in an email to Base64 imbedded source
        /// </summary>
        /// <param name="newMessage">MimeMessage that represents the loaded email message</param>
        /// <param name="bodyHtml">Body property of that message</param>
        /// <param name="imageResizeRatio">New ratio for image</param>
        /// <returns>String used for new body for UI to display</returns>
        private static string PopulateInlineImages(MimeMessage newMessage, string bodyHtml, double imageResizeRatio = 1)
        {
            // encode as base64 for easier downloading/storage 
            if (bodyHtml != null)
            {
                foreach (MimePart att in newMessage.BodyParts)
                {
                    if (att.ContentId != null && att.Content != null && att.ContentType.MediaType == "image" && (bodyHtml.IndexOf("cid:" + att.ContentId, StringComparison.Ordinal) > -1))
                    {
                        byte[] b;
                        using (var mem = new MemoryStream())
                        {
                            att.Content.DecodeTo(mem);
                            b = ReduceSize(mem, imageResizeRatio);
                            //b = mem.ToArray();
                        }
                        string imageBase64 = "data:" + att.ContentType.MimeType + ";base64," + System.Convert.ToBase64String(b);
                        bodyHtml = bodyHtml.Replace("cid:" + att.ContentId, imageBase64);
                    }
                }
            }
            return bodyHtml;
        }

        private static byte[] ReduceSize(Stream stream, double imageResizeRatio = 1)
        {
            Image source = Image.FromStream(stream);
            Image thumbnail = source.GetThumbnailImage((int)(source.Width * imageResizeRatio), (int)(source.Height * imageResizeRatio), AbortCallback, IntPtr.Zero);

            using (var memory = new MemoryStream())
            {
                thumbnail.Save(memory, source.RawFormat);
                return memory.ToArray();
            }
        }

        private static bool AbortCallback()
        {
            return false;
        }