Hi guys,
I created a Bitmap1 and filled it with RawColor4(1f, 0.0f, 0.0f, 0.5f) by this code:
D2D1.Bitmap1 img = new D2D1.Bitmap1(_graphics.D2D1Context5, new SharpDX.Size2(640, 480), new BitmapProperties1()
{
PixelFormat = new D2D1.PixelFormat(Format.R8G8B8A8_UNorm, D2D1.AlphaMode.Premultiplied),
DpiX = 96,
DpiY = 96,
BitmapOptions = BitmapOptions.Target
});
_graphics.D2D1Context5.Target = img;
_graphics.D2D1Context5.BeginDraw();
_graphics.D2D1Context5.AntialiasMode = AntialiasMode.Aliased;
//RawColor4 with Red=1f; G=0.0f; B=0.0f; Alpha = 0.5f;
SolidColorBrush br = new SolidColorBrush(_graphics.D2D1Context5, new SharpDX.Mathematics.Interop.RawColor4(1f, 0.0f, 0.0f, 0.5f));
_graphics.D2D1Context5.FillRoundedRectangle(new RoundedRectangle() { RadiusX = 20, RadiusY = 20, Rect = new SharpDX.Mathematics.Interop.RawRectangleF(10, 10, 630, 470) }, br);
br.Dispose();
_graphics.D2D1Context5.EndDraw();
Then i use this function to get Pixel value from img above:
private static Color4 GetPixel( Bitmap1 created_with_BitmapOption_Target, int x, int y)
{
var img1 = new D2D1.Bitmap1(_graphics.D2D1Context5, new SharpDX.Size2(created_with_BitmapOption_Target.PixelSize.Width, created_with_BitmapOption_Target.PixelSize.Height), new BitmapProperties1()
{
PixelFormat = new D2D1.PixelFormat(Format.R8G8B8A8_UNorm, D2D1.AlphaMode.Premultiplied),
DpiX = 96,
DpiY = 96,
BitmapOptions = BitmapOptions.CannotDraw | BitmapOptions.CpuRead
});
img1.CopyFromBitmap(created_with_BitmapOption_Target);
var map = img1.Map(MapOptions.Read);
var size = created_with_BitmapOption_Target.PixelSize.Width * created_with_BitmapOption_Target.PixelSize.Height * 4;
byte[] bytes = new byte[size];
Marshal.Copy(map.DataPointer, bytes, 0, size);
img1.Unmap();
img1.Dispose();
var position = (y * created_with_BitmapOption_Target.PixelSize.Width + x) * 4;
return new Color4(bytes[position], bytes[position + 1], bytes[position + 2], bytes[position + 3]);
}
Then i get pixel value :
Color4 c4val = GetPixel(img,50, 50);
I get c4value is: Alpha=127; Red=127; Green=0; Blue=0
This color is not the same the color i filled img (Red=1f; G=0.0f; B=0.0f; Alpha = 0.5f;)
Can anybody help where i was wrong in code?
Thank you so much in advance,
HoaHong