Added new feature: partial image drawing(see issue #95)

Currently only implemented in WPF.
This commit is contained in:
Luis Reis 2012-04-05 01:15:14 +01:00
Родитель fabc685eab
Коммит 9a960e399d
7 изменённых файлов: 188 добавлений и 25 удалений

Просмотреть файл

@ -66,6 +66,7 @@ namespace Samples
AddSample (n, "Transformations", typeof(DrawingTransforms));
AddSample (n, "Images and Patterns", typeof(DrawingPatternsAndImages));
AddSample (n, "Text", typeof(DrawingText));
AddSample (n, "Partial Images", typeof (PartialImages));
AddSample (null, "Frames", typeof(Frames));
AddSample (null, "Images", typeof(Images));

Просмотреть файл

@ -66,30 +66,81 @@
<Compile Include="MonoDevelop.Components.Chart\IntegerAxis.cs" />
<Compile Include="MonoDevelop.Components.Chart\Serie.cs" />
<Compile Include="MonoDevelop.Components.Chart\TickEnumerator.cs" />
<Compile Include="Samples\ScrollWindowSample.cs" />
<Compile Include="Samples\ButtonSample.cs" />
<Compile Include="Samples\DrawingTransforms.cs" />
<Compile Include="Samples\ComboBoxes.cs" />
<Compile Include="Samples\Designer.cs" />
<Compile Include="Samples\Windows.cs" />
<Compile Include="Samples\TextEntries.cs" />
<Compile Include="Samples\Images.cs" />
<Compile Include="Samples\CanvasWithWidget.cs" />
<Compile Include="Samples\Tables.cs" />
<Compile Include="Samples\Labels.cs" />
<Compile Include="Samples\Colors.cs" />
<Compile Include="Samples\Frames.cs" />
<Compile Include="Samples\Checkboxes.cs" />
<Compile Include="Samples\WidgetEvents.cs" />
<Compile Include="Samples\ClipboardSample.cs" />
<Compile Include="Samples\MenuSamples.cs" />
<Compile Include="Samples\Tooltips.cs" />
<Compile Include="Samples\TreeViews.cs" />
<Compile Include="Samples\PanedViews.cs" />
<Compile Include="Samples\DrawingText.cs" />
<Compile Include="Samples\DrawingFigures.cs" />
<Compile Include="Samples\DrawingPatternsAndImages.cs" />
<Compile Include="Samples\ListBoxSample.cs" />
<Compile Include="Samples\PartialImages.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ScrollWindowSample.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ButtonSample.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\DrawingTransforms.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ComboBoxes.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Designer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Windows.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\TextEntries.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Images.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\CanvasWithWidget.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Tables.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Labels.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Colors.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Frames.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Checkboxes.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\WidgetEvents.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ClipboardSample.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\MenuSamples.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\Tooltips.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\TreeViews.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\PanedViews.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\DrawingText.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\DrawingFigures.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\DrawingPatternsAndImages.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Samples\ListBoxSample.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@ -118,4 +169,4 @@
</Properties>
</MonoDevelop>
</ProjectExtensions>
</Project>
</Project>

Просмотреть файл

@ -0,0 +1,57 @@
//
// PartialImages.cs
//
// Author:
// Luís Reis <luiscubal@gmail.com>
//
// Copyright (c) 2012 Luís Reis
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt;
using Xwt.Drawing;
namespace Samples
{
public class PartialImages: VBox
{
public PartialImages ()
{
PartialImageCanvas canvas = new PartialImageCanvas ();
PackStart (canvas, BoxMode.FillAndExpand);
}
}
internal class PartialImageCanvas : Canvas
{
private Image img;
public PartialImageCanvas ()
{
img = Image.FromResource (GetType (), "cow.jpg");
}
protected override void OnDraw (Context ctx)
{
base.OnDraw (ctx);
ctx.DrawImage (img, new Rectangle (10, 10, 50, 50), new Rectangle (0, 0, 50, 50));
}
}
}

Просмотреть файл

@ -5,6 +5,7 @@
// Eric Maupin <ermau@xamarin.com>
// Hywel Thomas <hywel.w.thomas@gmail.com>
// Lytico (http://limada.sourceforge.net)
// Luís Reis <luiscubal@gmail.com>
//
// Copyright (c) 2012 Xamarin, Inc.
//
@ -300,6 +301,15 @@ namespace Xwt.WPFBackend
DrawImageCore (c.Graphics, bmp, (float) x, (float) y, (float) width, (float) height, (float) alpha);
}
public void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha)
{
var c = (DrawingContext) backend;
Bitmap bmp = DataConverter.AsBitmap (img);
DrawImageCore (c.Graphics, bmp, srcRect, destRect, (float) alpha);
}
public void ResetTransform (object backend)
{
var c = (DrawingContext)backend;
@ -406,5 +416,32 @@ namespace Xwt.WPFBackend
else
g.DrawImage (bmp, x, y, width, height);
}
internal void DrawImageCore (Graphics g, Bitmap bmp, Rectangle srcRect, Rectangle destRect, float alpha)
{
if (alpha < 1)
{
var attr = new ImageAttributes ();
float[][] matrixItems = new[] {
new float[] { 1, 0, 0, 0, 0 },
new float[] { 0, 1, 0, 0, 0 },
new float[] { 0, 0, 1, 0, 0 },
new float[] { 0, 0, 0, alpha, 0 },
new float[] { 0, 0, 0, 0, 1 },
};
attr.SetColorMatrix (new ColorMatrix (matrixItems), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
PointF[] points = new PointF[3];
points[0] = new PointF ((float) destRect.X, (float) destRect.Y);
points[1] = new PointF ((float) (destRect.X + destRect.Width), (float) destRect.Y);
points[2] = new PointF ((float) destRect.X, (float) (destRect.Y + destRect.Height));
g.DrawImage (bmp, points, srcRect.ToSDRectF (), GraphicsUnit.Pixel, attr);
}
else
g.DrawImage (bmp, destRect.ToSDRectF (), srcRect.ToSDRectF (), GraphicsUnit.Pixel);
}
}
}

Просмотреть файл

@ -61,6 +61,11 @@ namespace Xwt.WPFBackend
return new SW.Rect (rect.X, rect.Y, rect.Width, rect.Height);
}
public static SD.RectangleF ToSDRectF (this Rectangle rect)
{
return new SD.RectangleF ((float) rect.X, (float) rect.Y, (float) rect.Width, (float) rect.Height);
}
public static Int32Rect ToInt32Rect (this Rectangle rect)
{
return new Int32Rect ((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height);

Просмотреть файл

@ -86,6 +86,8 @@ namespace Xwt.Backends
void DrawImage (object backend, object img, double x, double y, double width, double height, double alpha);
void DrawImage (object backend, object img, Rectangle srcRect, Rectangle destRect, double alpha);
void ResetTransform (object backend);
void Rotate (object backend, double angle);

Просмотреть файл

@ -374,6 +374,16 @@ namespace Xwt.Drawing
handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, alpha);
}
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect)
{
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, 1);
}
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect, double alpha)
{
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha);
}
/// <summary>
/// Resets the current trasnformation matrix (CTM) to the Identity Matrix
/// </summary>