天天看點

WPF WriteableBitmap的使用

選中項目按右鍵添加-資源檔案-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效果

WPF WriteableBitmap的使用

繼續閱讀