简体   繁体   English

C#与Java的java.awt.image.DataBuffer等效

[英]C# equivilent to Java's java.awt.image.DataBuffer

Here's my Java Code: 这是我的Java代码:

import java.awt.image.DataBuffer;

public class B extends DataBuffer
{
  public float[][] a;
  public float[] b;

  public float[] a()
  {
    return this.b;
  }
}

Question is plain and simple. 问题很简单。 What is the C# equivalent to java.awt.image.DataBuffer? C#等同于java.awt.image.DataBuffer是什么?

Or do I need to back up one level and find the equivalent to java.awt.image? 还是我需要备份一个级别并找到与java.awt.image等效的级别?

TIA, TIA,

KeithC 基思

It sounds like you are trying to do some sort of image manipulation. 听起来您正在尝试进行某种图像处理。 You seem to need direct access to the pixel data for a bitmap because method calls would be too slow. 您似乎需要直接访问位图的像素数据,因为方法调用太慢了。

.NET provides Bitmap.LockBits for this purpose. .NET为此提供了Bitmap.LockBits Here's an example how you might use this: 这是一个示例,您可以如何使用它:

var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
unsafe
{
    var data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    for (int y = 0; y < height; y++)
    {
        var b = (byte*) data.Scan0 + y * data.Stride;
        for (int x = 0; x < width; x++)
        {
            var blue = b[4 * x];
            var green = b[4 * x + 1];
            var red = b[4 * x + 2];
            var alpha = b[4 * x + 3];

            // ... do whatever you want with these values ...
        }
    }
    bmp.UnlockBits(data);
}
return bmp;

In order to use this, you need to enable unsafe code in your project. 为了使用此功能,您需要在项目中启用不安全的代码 In the project properties, on the “Build” tab, enable the option Allow unsafe code . 在项目属性的“构建”选项卡上,启用选项允许不安全代码

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

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