728x90
728x170
▶ GraphicsDrawable.cs
using System.Reflection;
using Microsoft.Maui.Graphics.Platform;
namespace TestProject;
/// <summary>
/// 그래픽스 그리기 가능형
/// </summary>
public class GraphicsDrawable : IDrawable
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 그리기 - Draw(canvas, dirtyRectangle)
/// <summary>
/// 그리기
/// </summary>
/// <param name="canvas">캔버스</param>
/// <param name="dirtyRectangle">더티 사각형</param>
public void Draw(ICanvas canvas, RectF dirtyRectangle)
{
canvas.FillColor = Colors.Yellow;
canvas.FillRectangle(dirtyRectangle);
Assembly assembly = GetType().GetTypeInfo().Assembly;
using(Stream stream = assembly.GetManifestResourceStream("TestProject.IMAGE.sample.jpg"))
{
Microsoft.Maui.Graphics.IImage image = PlatformImage.FromStream(stream);
float width;
float height;
float offset;
if(image.Width > image.Height)
{
width = 250;
height = 250 * image.Height / image.Width;
offset = (width - height) / 2;
canvas.DrawImage(image, 50, 50 + offset, width, height);
}
else
{
height = 250;
width = 250 * image.Width / image.Height;
offset = height - width;
canvas.DrawImage(image, 50 + offset, 50, width, height);
}
}
}
#endregion
}
728x90
▶ MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="TestProject.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestProject">
<ContentPage.Resources>
<local:GraphicsDrawable x:Key="GraphicsDrawableKey" />
</ContentPage.Resources>
<GraphicsView
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="350"
HeightRequest="350"
Drawable="{StaticResource GraphicsDrawableKey}" />
</ContentPage>
728x90
그리드형(광고전용)