Hi @Mahdi Hosseini ,
Welcome to Microsoft Q&A!
I'll share my solution here, it may not be elegant enough.
- Install System.Drawing.Common in NuGet Manager, use Graphics.DrawString to draw text.
- Rotate the image.
- Use the image in ImageBrush , fill it in a layout.
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="Winui3WaterMark.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Winui3WaterMark"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1">
<Rectangle x:Name="rectangle" Height="500" Width="500">
<Rectangle.Fill>
<ImageBrush ImageSource="{x:Bind imgTest.Source}" Stretch="None"/>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</Window>
using ABI.Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Winui3WaterMark
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public Microsoft.UI.Xaml.Controls.Image imgTest;
public MainWindow()
{
this.InitializeComponent();
imgTest=new Microsoft.UI.Xaml.Controls.Image();
var bmp = ConvertTextToImage("WaterMark", "Calibri", 10, Color.Empty, Color.HotPink, 1000, 2000);
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream stream = new MemoryStream())
{
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
bitmapImage.SetSource(stream.AsRandomAccessStream());
}
imgTest.Source = bitmapImage;
}
public Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
{
Bitmap bmp = new Bitmap(width, Height);
//a way to get the width of string
SizeF size;
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
size = graphics.MeasureString(txt, new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Point));
}
//draw text
using (Graphics graphics = Graphics.FromImage(bmp))
{
Font font = new Font(fontname, fontsize);
//In order to avoid blank areas after rotation, expand the drawing range
graphics.FillRectangle(new SolidBrush(bgcolor), -size.Width, -font.Height, width+2*size.Width, Height+2*font.Height);
for (int i = 0; i < Height/ font.Height; i++)
{
for (int j = -2; j< width/ size.Width; j++ )
{
graphics.DrawString(txt, font, new SolidBrush(fcolor), j* size.Width, i * font.Height);
}
}
graphics.Flush();
font.Dispose();
graphics.Dispose();
}
//rotate the bitmap
Bitmap returnBitmap = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(returnBitmap))
{
g.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
g.RotateTransform(15);
g.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
g.DrawImage(bmp, new System.Drawing.Point(0, 0));
}
return returnBitmap;
}
}
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.