Convert Java Code of Bitmap into C# code In Console app

197 Views Asked by At

I am trying to convert Java code into c# So here is Java code

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap decodeStream = BitmapFactory.decodeStream(openInputStream, null, options);

Then I am saving this bitmap by

File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");


    try (FileOutputStream out = new FileOutputStream(dest)) {
        decodeStream.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
        
    } catch (IOException e) {
        e.printStackTrace();
    }

Now can anyone help me to convert this code into c#. I Tried to import Xamrine Android DLL but got success no far. I am

1

There are 1 best solutions below

2
MrMoeinM On

As far as I understand you only need to load simple jpg image. That is all your java code do.

If you want to load jpg image from stream you can use

Bitmap.FromStream()

e.g.

using (FileStream fs = new FileStream(@"Image Address.jpg", FileMode.Open, FileAccess.Read))
{
    var decodeStream = Bitmap.FromStream(fs);
}

of course you can open your image without stream too.

var image = Bitmap.FromFile(@"Image Address.jpg");

So your code will be something like this

File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");
var image = Bitmap.FromFile(dest.FullName);

You can not access Bitmap class out of the box.

For dot net core install package System.Drawing.Common.

For dot net framework add reference to System.Drawing.

For Xamarin see this answer https://stackoverflow.com/a/34869330/5964792