SilverlightにBitmapImageインスタンスがあり、画像の各ピクセルの色情報を読み取る方法を見つけようとしています。これどうやってするの?私はあなたが渡す配列にピクセル情報を書き込むこのクラスのCopyPixels()
メソッドがあることがわかりますが、その配列から色情報を読み取る方法はわかりません。SilverlightでBitmapImageからピクセルの色を読み取るにはどうすればよいですか?
どうすればいいですか?
SilverlightにBitmapImageインスタンスがあり、画像の各ピクセルの色情報を読み取る方法を見つけようとしています。これどうやってするの?私はあなたが渡す配列にピクセル情報を書き込むこのクラスのCopyPixels()
メソッドがあることがわかりますが、その配列から色情報を読み取る方法はわかりません。SilverlightでBitmapImageからピクセルの色を読み取るにはどうすればよいですか?
どうすればいいですか?
現在リリースされているSilverlight 3ベータ版の現在のBitmap APIでは不可能です。
Silverlight BitmapImageファイルには、CopyPixelsメソッドがありません。 MSDNのドキュメントhereを参照してください。
Silverlight 3でWriteableBitmap
クラスを探します。このクラスには、ビットマップのピクセルデータをintの配列に戻すメンバーPixels
があります。
トランスフォームを使用した例。 bi
はBitmapImage
オブジェクトです。 WriteableBitmap bmp = new WriteableBitmap(bitmapImageObj);
:
Image img = new Image();
img.source = bi;
img.Measure(new Size(100, 100));
img.Arrange(new Rect(0, 0, 100, 100));
ScaleTransform scaleTrans = new ScaleTransform();
double scale = (double)500/(double)Math.Max(bi.PixelHeight, bi.PixelWidth);
scaleTrans.CenterX = 0;
scaleTrans.CenterY = 0;
scaleTrans.ScaleX = scale;
scaleTrans.ScaleY = scale;
WriteableBitmap writeableBitmap = new WriteableBitmap(500, 500);
writeableBitmap.Render(img, scaleTrans);
int[] pixelData = writeableBitmap.Pixels;
まず、あなたは、ピクセルのコレクションを取得するためにWritableBitmap
を使用する必要があります。各ピクセルは32ビットの整数として表されます。私は整数を4バイト(ARGB)に分割するのに役立つ構造を作っています。ここで
struct BitmapPixel
{
public byte A;
public byte R;
public byte G;
public byte B;
public BitmapPixel(int pixel)
: this(BitConverter.GetBytes(pixel))
{
}
public BitmapPixel(byte[] pixel)
: this(pixel[3], pixel[2], pixel[1], pixel[0])
{
}
public BitmapPixel(byte a, byte r, byte g, byte b)
{
this.A = a;
this.R = r;
this.G = g;
this.B = b;
}
public int ToInt32()
{
byte[] pixel = new byte[4] { this.B, this.G, this.R, this.A };
return BitConverter.ToInt32(pixel, 0);
}
}
は、赤の値を変更するために使用することができる方法の例です:
BitmapPixel pixel = new BitmapPixel(bmp.Pixels[0]);
pixel.R = 255;
bmp.Pixels[0] = pixel.ToInt32();
また、私はWriteableBitmap.PixelsがプリマルチプライRGB形式であることを言及したいと思います。 Thisの記事では、その意味を説明します。それは、MSDNにある
public static void CompensateRGB(int[] pixels)
{
for (int i = 0; i < pixels.Length; i++)
{
BitmapPixel pixel = new BitmapPixel(pixels[i]);
if (pixel.A == 255 || pixel.A == 0)
continue;
if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0)
{
// color is unrecoverable, get rid of this
// pixel by making it transparent
pixel.A = 0;
}
else
{
double factor = 255.0/pixel.A;
pixel.A = 255;
pixel.R = (byte)(pixel.R * factor);
pixel.G = (byte)(pixel.G * factor);
pixel.B = (byte)(pixel.B * factor);
}
pixels[i] = pixel.ToInt32();
}
}
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage_methods.aspx :そして、ここでどのようにBitmapPixel構造を使用して行わ補償でありますしかし、はい、それはSilverlightで利用可能ではないようです。 – Rafe
あなたが提供したリンクはSilverlightではなく.NET 3.5用です。 Silverlight BitmapImageクラスのMSDNドキュメントは、次のURLから入手できます。http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(VS.96).aspx – markti