如何在xamarin.forms中将流转换为位图以调整图像对比度 [英] How to convert stream to bitmap in xamarin.forms to adjust image contrast

查看:174
本文介绍了如何在xamarin.forms中将流转换为位图以调整图像对比度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

I am trying to raise the contrast of an image in Xamarin.forms using this algorithm. My code gives me in image in the form of a System.IO.Stream, which I want to convert to a Bitmap/LockBitmap to use the algorithm.

我使用System.Drawing 尝试了诸如之类的代码,该代码无法正常工作.错误提示找不到类型或名称空间位图".然后,我尝试使用Android.Graphics 修复了该错误,但是我不知道它是否可以与需要在Android和iOS上运行的Xamarin.forms应用配合使用.

I tried code such as using System.Drawing, which didn't work. The error said "the type or namespace bitmap could not be found." Then, I tried using Android.Graphics, which fixes that error, but I don't know if it will work with a Xamarin.forms app that needs to run on Android and iOS.

即使那行得通,我实际上如何将System.IO.Stream转换为位图?还有另一种不需要位图的算法吗?

Even if that does work, how do I actually convert a System.IO.Stream into a Bitmap? Is there another algorithm that doesn't require bitmaps?

我想我可以使用BitmapFactory将流转换为位图.

I think I can use BitmapFactory to convert the stream into a bitmap.

推荐答案

您可以使用

you could use DependencyService to convert System.IO.Stream into Bitmap ,after raise the contrast of an image ,return the new stream,and show the Image in forms page.

喜欢:

创建接口 IRaiseImage.cs :

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

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

then in Droid.project,creat 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;
      }
   }
}

然后您可以将其设置为表单页面中的图像":

then you could set to the Image in your forms page:

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

这篇关于如何在xamarin.forms中将流转换为位图以调整图像对比度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆