Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

c# - How to convert stream to bitmap in xamarin.forms to adjust image contrast

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.

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.

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

like:

create a interface IRaiseImage.cs:

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

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...