【问题标题】:How to convert stream to bitmap in xamarin.forms to adjust image contrast如何在 xamarin.forms 中将流转换为位图以调整图像对比度
【发布时间】:2019-08-05 15:05:22
【问题描述】:

我正在尝试使用this 算法提高 Xamarin.forms 中图像的对比度。我的代码以 System.IO.Stream 的形式为我提供了图像,我想将其转换为 Bitmap/LockBitmap 以使用该算法。

我尝试了using System.Drawing 之类的代码,但没有成功。错误说“找不到类型或命名空间位图”。然后,我尝试了using Android.Graphics,它修复了该错误,但我不知道它是否适用于需要在 Android 和 iOS 上运行的 Xamarin.forms 应用。

即使这确实有效,我如何实际将 System.IO.Stream 转换为位图?还有其他不需要位图的算法吗?

编辑:我想我可以使用 BitmapFactory 将流转换为位图。

【问题讨论】:

  • 使用 Bitmap.FromStream(stream)
  • 我试过了,但它不起作用。我认为问题在于一个是 Android.Graphics.Bitmap 并且该方法可能是 System.Drawing.Bitmap 的一部分。
  • 尝试解码流。请参阅:docs.microsoft.com/en-us/dotnet/api/…
  • 是的!这就是我最终所做的。

标签: c# xamarin.forms bitmap


【解决方案1】:

您可以使用DependencyServiceSystem.IO.Stream转换为Bitmap,提高图像对比度后,返回新流,并在表单页面显示Image

喜欢:

创建接口IRaiseImage.cs:

public interface IRaiseImage
{
    Stream RaiseImage(Stream stream);
}

然后在 Droid.project 中,创建 AndroidRaiseImage.cs:

[assembly: Dependency(typeof(AndroidRaiseImage))]
namespace App18.Droid
{
  class AndroidRaiseImage : IRaiseImage
   {
     public Stream RaiseImage(Stream stream)
      {
        Bitmap bitmap = BitmapFactory.DecodeStream(stream);
        //raise the bitmap contrast
        ...
        // return the new stream
        MemoryStream ms = new MemoryStream();
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms);
        return ms;
      }
   }
}

然后您可以在表单页面中设置图片:

MemoryStream memoryStream = new MemoryStream();//your original image stream
Image image = new Image();
image.Source = ImageSource.FromStream(() => DependencyService.Get<IRaiseImage>().RaiseImage(memoryStream));

【讨论】:

  • 感谢您的回答,但我之前使用jdweng的评论和bitmapfactory解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多