Как нарисовать часть изображения в Maui.GraphicsC#

Место общения программистов C#
Ответить
Гость
 Как нарисовать часть изображения в Maui.Graphics

Сообщение Гость »

В свойстве IDrawable Drawable MAUI.GraphicsView я хочу нарисовать часть изображения. Это субпрямоугольник исходного изображения.
Проблема: я не могу найти способ обрезать IImage, возвращаемый PlatformImage.FromStream(stream) ни рисовать только часть IImage, используя что-то вроде этой функции Skiasharp, но для Maui.GraphicsView:
public void DrawImage(SkiaSharp.SKImage image, SkiaSharp.SKRect source, SkiaSharp.SKRect dest);

Может ли кто-нибудь предложить способ достижения этой цели?
Для подробностей, вот код, который я хочу расширить: public class GraphicsDrawable : IDrawable
{
public double Scale { get; set; }
public Point Center { get; set; }
public Rect DisplayedRect { get; set; }

private Microsoft.Maui.Graphics.IImage image;

public GraphicsDrawable()
{
// Load image
Assembly assembly = GetType().GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream("BOBMaui.Resources.Images.bobstart.jpg"))
{
image = PlatformImage.FromStream(stream);
}
}

public void Draw(ICanvas canvas, RectF containerRect)
{
if (image != null)
{
// Anyway, the output will be displayed on containerRect size which is the full size of the container

// STEP 1 - Computes the clipping to apply to image
// Computes scaled size which is the size of the part of the image to be displayed
double scaledWidth = image.Width / Scale;
double scaledHeight = image.Height / Scale;

// Computes horizontal coordinates
double xCenterImageOUT = Center.X * image.Width;
double xLeft = Math.Max(0, xCenterImageOUT - scaledWidth/2); // May not be negative
double xRight = Math.Min(xCenterImageOUT + scaledWidth / 2, image.Width); // May not extend outside base image
if (xRight - xLeft < scaledWidth)
{
// One of the border was forced to 0 or image.Width
if (xLeft == 0) xRight = scaledWidth;
else xLeft = image.Width - scaledWidth;
}

// Computes vertical coordinates
double yCenterImageOUT = Center.Y * image.Height;
double yTop = Math.Max(0, yCenterImageOUT - scaledHeight / 2); // May not be negative
double yBottom = Math.Min(yCenterImageOUT + scaledHeight / 2, image.Height); // May not extend outside base image
if (yBottom - yTop < scaledHeight)
{
// One of the border was forced to 0 or image.Width
if (yTop == 0) yBottom = scaledHeight;
else yBottom = image.Height - scaledHeight;
}
// Define clipping rectangle
DisplayedRect = new Rect(xLeft, yTop, scaledWidth, scaledHeight);

// STEP 2 - Paint the canvas
// There is the problem - I need to crop the image before drawing it
// Sthg like: canvas.DrawImage(image, source: DisplayRect, dest: containerRect);
canvas.DrawImage(image, containerRect.X, containerRect.Y, containerRect.Width, containerRect.Height);
}

}
}


Подробнее здесь: https://stackoverflow.com/questions/781 ... i-graphics
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»