简体   繁体   English

在控制台应用程序中将位图的 Java 代码转换为 C# 代码

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

I am trying to convert Java code into c# So here is Java code我正在尝试将 Java 代码转换为 c# 所以这里是 Java 代码

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#.现在任何人都可以帮助我将此代码转换为 c#。 I Tried to import Xamrine Android DLL but got success no far.我试图导入 Xamrine Android DLL 但很快就成功了。 I am我是

As far as I understand you only need to load simple jpg image.据我了解,您只需要加载简单的 jpg 图像。 That is all your java code do.这就是您的所有 Java 代码。

If you want to load jpg image from stream you can use如果要从流加载 jpg 图像,可以使用

Bitmap.FromStream()

eg例如

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.您无法立即访问 Bitmap 类。

For dot net core install package System.Drawing.Common.对于 dot net core 安装包 System.Drawing.Common。

For dot net framework add reference to System.Drawing.对于点网框架,添加对 System.Drawing 的引用。

For Xamarin see this answer https://stackoverflow.com/a/34869330/5964792对于 Xamarin,请参阅此答案https://stackoverflow.com/a/34869330/5964792

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM