选中项目按右键添加-资源文件-WPF-窗口
generate_bitmap.xml文件内容为
<Window x:Class="WpfApp1.generate_bitmap"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="generate_bitmap" Height="450" Width="800">
<Grid x:Name="Layout_Root" Background="White">
<Grid.RowDefinitions >
<!--定义行-->
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<Button Content="Button" Grid.Row="1" Height="81" HorizontalAlignment="Left" Margin="106,90,0,0" Name="button1" VerticalAlignment="Top" Width="193"></Button>
<Button HorizontalAlignment="Center" Content="Generate_Bitmap" Width="120" Margin="5" Padding="10" Click="Button_Click" ></Button>
<Image Grid.Row="1" x:Name="img" Margin="5" Width="400" Height="300" IsHitTestVisible="False"></Image>
</Grid>
</Window>
下面是generate_bitmap.xaml.cs文件内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// generate_bitmap.xaml 的交互逻辑
/// </summary>
public partial class generate_bitmap : Window
{
public generate_bitmap()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap((int)img.Width, (int)img.Height, 96, 96, PixelFormats.Bgr32, null);
Int32Rect rect = new Int32Rect(0,0,(int)img.Width, (int)img.Height);
byte[] pixels = new byte[(int)img.Width * (int)img.Height * wb.Format.BitsPerPixel / 8];
Random rand = new Random();
for(int y = 0; y < wb.PixelHeight; y++)
{
for(int x = 0; x < wb.PixelWidth; x++)
{
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
// Determine the pixel's color.
if ((x % 5 == 0) || (y % 7 == 0))
{
red = (int)((double)y / wb.PixelHeight * 255);
green = rand.Next(100, 255);
blue = (int)((double)x / wb.PixelWidth * 255);
alpha = 255;
}
else
{
red = (int)((double)x / wb.PixelWidth * 255);
green = rand.Next(100, 255);
blue = (int)((double)y / wb.PixelHeight * 255);
alpha = 50;
}
int pixelOffset = (x + y * wb.PixelWidth) * wb.Format.BitsPerPixel / 8;
pixels[pixelOffset] = (byte)blue;
pixels[pixelOffset + 1] = (byte)green;
pixels[pixelOffset + 2] = (byte)red;
pixels[pixelOffset + 3] = (byte)alpha;
}
int stride = (wb.PixelWidth * wb.Format.BitsPerPixel) / 8;
wb.WritePixels(rect, pixels, stride, 0);
}
img.Source = wb;
}
}
}
下面是启动duihuakua对话框的代码
private void Button_Click(object sender, RoutedEventArgs e)
{
Type type = this.GetType();
Assembly assembly = type.Assembly;
//根据window类的名称获取window对象
Window window=(Window)assembly.CreateInstance("WpfApp1.generate_bitmap");
window.Show();
}
运行xia效果