Я работаю над WriteableBitmaps
, и у меня есть метод, который:
- Копирует
WriteableBitmap
из параметра в переменную, которая находится за пределами метода - Работает на первом растровом изображении
- Добавляет скопированное растровое изображение в
UndoStack
Смысл этого метода состоит в том, чтобы вносить изменения в растровое изображение и после этого добавлять в отмену растровое растровое изображение перед изменениями.
Если этометод вызывается очень часто и вызывает OutOfMemoryException
.
переменные
private WriteableBitmap _oldBitmap;
private Image _oldImage;
метод
public Layer ExecuteTool(Layer layer, Coordinates startingCoords, Color color,int toolSize, ToolType tool)
{
if (toolSize < 1) return null;
Layer cLayer = layer;
_oldBitmap = new WriteableBitmap(layer.LayerBitmap);
_oldImage = layer.LayerImage;
_oldImage.Source = _oldBitmap;
switch (tool)
{
case ToolType.Pen:
cLayer.LayerBitmap = DrawPixel(cLayer.LayerBitmap, startingCoords, toolSize,color);
break;
case ToolType.Bucket:
cLayer.LayerBitmap = FloodFill(cLayer.LayerBitmap, startingCoords, color);
break;
case ToolType.Line:
if (_toolIsExecuting == false)
{
LineAsync(cLayer, startingCoords, color, toolSize);
}
break;
case ToolType.Circle:
if(_toolIsExecuting == false)
{
CircleAsync(cLayer, startingCoords, color);
}
break;
case ToolType.Rectangle:
if(_toolIsExecuting == false)
{
RectangleAsync(cLayer, startingCoords, color);
}
break;
case ToolType.Earser:
cLayer.LayerBitmap = DrawPixel(cLayer.LayerBitmap, startingCoords, toolSize, Colors.Transparent);
break;
case ToolType.Lighten:
if(Mouse.LeftButton == MouseButtonState.Pressed)
{
cLayer.LayerBitmap = Lighten(cLayer.LayerBitmap, startingCoords);
}
else if(Mouse.RightButton == MouseButtonState.Pressed)
{
cLayer.LayerBitmap = Darken(cLayer.LayerBitmap, startingCoords);
}
break;
default:
break;
}
if (tool != ToolType.ColorPicker)
{
UndoManager.RecordChanges("ActiveLayer", new Layer(_oldBitmap, _oldImage), cLayer, string.Format("{0} Tool.", tool.ToString()));
}
return cLayer;
}
PS,Не работает без копирования растрового изображения