Lock() public method

Locks the data of the image to directly access the bytes of the image
This locks the data to read and write to directly using unsafe pointers. After reading or updating the data, you must call BitmapData.Dispose() to unlock the data before using the bitmap. e.g.: using (var bd = bitmap.Lock ()) { byte* pdata = bd.Data; // access data }
public Lock ( ) : Eto.Drawing.BitmapData
return Eto.Drawing.BitmapData
Example #1
0
		void DrawTest(Bitmap image)
		{
			// should always ensure .Dispose() is called when you are done with a Graphics or BitmapData object.

			// Test setting pixels directly
			using (var bd = image.Lock())
			{
				var sz = image.Size / 5;
				for (int x = sz.Width; x < sz.Width * 2; x++)
					for (int y = sz.Height; y < sz.Height * 2; y++)
						bd.SetPixel(x, y, Colors.Green);
			}

			// Test using Graphics object
			using (var graphics = new Graphics(image))
			{
				graphics.DrawLine(Pens.Blue, Point.Empty, new Point(image.Size));
				graphics.DrawRectangle(Pens.Blue, new Rectangle(image.Size - 1));
			}

			// should be able to set pixels after using graphics object
			using (var bd = image.Lock())
			{
				var sz = image.Size / 5;
				for (int x = sz.Width * 3; x < sz.Width * 4; x++)
					for (int y = sz.Height * 3; y < sz.Height * 4; y++)
						bd.SetPixel(x, y, Colors.Red);
			}

		}
Example #2
0
        public static ed.Image ToEto(this IImage cImage)
        {
            var eImage = new ed.Bitmap(cImage.Size.Width, cImage.Size.Height, ed.PixelFormat.Format32bppRgba);

            using (var eData = eImage.Lock())
            {
                if (eData.ScanWidth != cImage.Size.Width * 4)
                {
                    throw new NotImplementedException();
                }
                var rawData = cImage.GetRawData();
                Marshal.Copy(rawData, 0, eData.Data, rawData.Length);
            }
            return(eImage);
        }
Example #3
0
        private ed.Bitmap LockEtoBitmap()
        {
            sdGraphics?.Flush();

            var sdData        = sdBuffer.LockBits(new sd.Rectangle(0, 0, sdBuffer.Width, sdBuffer.Height), sd.Imaging.ImageLockMode.ReadOnly, sdBuffer.PixelFormat);
            var bytesPerPixel = ((int)sdBuffer.PixelFormat >> 11) & 31;
            var byteLength    = sdData.Height * sdData.Width * bytesPerPixel;

            var etoBuffer = new ed.Bitmap(sdBuffer.Width, sdBuffer.Height, ed.PixelFormat.Format32bppRgba);
            var etoData   = etoBuffer.Lock();

            if (sdData.Stride < 0)
            {
                throw new Exception("Negative stride value encountered!");
            }

            unsafe
            {
                if (sdData.Stride > 0 && sdData.Stride == sdData.Width * bytesPerPixel)
                {
                    Buffer.MemoryCopy((void *)sdData.Scan0, (void *)etoData.Data, byteLength, byteLength);
                }
                else // Slightly slower route using the given stride width
                {
                    var switchColors     = MColor.ShouldSwitchColors;
                    var scan             = (byte *)sdData.Scan0;
                    var bytesPerScanLine = sdData.Width * bytesPerPixel;
                    Parallel.For(0, sdData.Height, i =>
                    {
                        var line = scan + (i * sdData.Stride);
                        for (int j = 0; j < bytesPerScanLine; j += bytesPerPixel)
                        {
                            var b = line[j];
                            var g = line[j + 1];
                            var r = line[j + 2];
                            var a = line[j + 3];
                            var c = ed.Color.FromArgb(switchColors ? b : r, g, switchColors ? r : b, a);
                            etoData.SetPixel(j / bytesPerPixel, i, c);
                        }
                    });
                }
            }

            etoData.Dispose();
            sdBuffer.UnlockBits(sdData);

            return(etoBuffer);
        }
Example #4
0
		static void ValidateImages(Bitmap image, Bitmap clone, Rectangle? rect = null)
		{
			var testRect = rect ?? new Rectangle(image.Size);
			using (var imageData = image.Lock())
			using (var cloneData = clone.Lock())
			{
				if (imageData.BytesPerPixel == 1)
				{
					unsafe
					{
						// test pixels directly
						byte* imageptr = (byte*)imageData.Data;
						imageptr += testRect.Top * imageData.ScanWidth + testRect.Left;
						byte* cloneptr = (byte*)cloneData.Data;
						for (int y = 0; y < testRect.Height; y++)
						{
							byte* imagerow = imageptr;
							byte* clonerow = cloneptr;
							for (int x = 0; x < testRect.Width; x++)
							{
								var imagePixel = *(imagerow++);
								var clonePixel = *(clonerow++);
								if (imagePixel != clonePixel)
								{
									Assert.Fail("Image pixels are not the same at position {0},{1} (source: {2}, clone: {3})", x, y, imagePixel, clonePixel);
								}
							}
							imageptr += imageData.ScanWidth;
							cloneptr += cloneData.ScanWidth;
						}
					}
				}
				else
					for (int x = 0; x < testRect.Width; x++)
						for (int y = 0; y < testRect.Height; y++)
						{
							var imagePixel = imageData.GetPixel(x + testRect.Left, y + testRect.Top);
							var clonePixel = cloneData.GetPixel(x, y);
							if (imagePixel != clonePixel)
								Assert.Fail("Image pixels are not the same at position {0},{1} (source: {2}, clone: {3})", x, y, imagePixel, clonePixel);
						}
			}
		}