public void Clear(Color c) { _bitmap.Lock(); for (Int32 y = 0; y < _bitmap.Height; y++) { for (Int32 x = 0; x < _bitmap.Width; x++) { _bitmap.SetPixel(x, y, c); } } _bitmap.Unlock(); }
public void CopyTo(FastBitmap bitmap, Int32 destx, Int32 desty, Int32 srcx, Int32 srcy, Int32 width, Int32 height) { try { Lock(); bitmap.Lock(); for (Int32 y = 0; y < height; y++) { for (Int32 x = 0; x < width; x++) { Color c = GetPixel(srcx + x, srcy + y); bitmap.SetPixel(destx + x, desty + y, c); } } } finally { Unlock(); bitmap.Unlock(); } }
internal FastBitmap Flatten() { // create a bitmap for result image FastBitmap final = new FastBitmap(_width, _height, PixelFormat.Format24bppRgb); // lock all bitmaps final.Lock(); for (Int32 i = 0; i < _layers.Count; i++) { Layer l = _layers[i]; l._bitmap.Lock(); if (l.Mask != null) { l.Mask.Lock(); } } // calculate colors of flattened image // 1. take offsetx, offsety into consideration // 2. calculate alpha of color (alpha, opacity, mask) // 3. mix colors of current layer and layer below for (Int32 y = 0; y < _height; y++) { for (Int32 x = 0; x < _width; x++) { Color c0 = _layers[0]._bitmap.GetPixel(x, y); for (Int32 i = 1; i < _layers.Count; i++) { Layer layer = _layers[i]; Color c1 = Color.Transparent; if (x >= layer.OffsetX && x <= layer.OffsetX + layer._bitmap.Width - 1 && y >= layer.OffsetY && y <= layer.OffsetY + layer._bitmap.Height - 1) { c1 = layer._bitmap.GetPixel(x - layer.OffsetX, y - layer.OffsetY); } if (c1.A == 255 && layer.Opacity == 1.0 && layer.Mask == null) { c0 = c1; } else { Double tr, tg, tb, a; a = c1.A / 255.0 * layer.Opacity; if (layer.Mask != null) { a *= layer.Mask.GetIntensity(x, y) / 255.0; } tr = c1.R * a + c0.R * (1.0 - a); tg = c1.G * a + c0.G * (1.0 - a); tb = c1.B * a + c0.B * (1.0 - a); tr = Math.Round(tr); tg = Math.Round(tg); tb = Math.Round(tb); tr = Math.Min(tr, 255); tg = Math.Min(tg, 255); tb = Math.Min(tb, 255); c0 = Color.FromArgb((Byte)tr, (Byte)tg, (Byte)tb); } } final.SetPixel(x, y, c0); } } // unlock all bitmaps for (Int32 i = 0; i < _layers.Count; i++) { Layer l = _layers[i]; l._bitmap.Unlock(); if (l.Mask != null) { l.Mask.Unlock(); } } final.Unlock(); return(final); }