WPF WriteableBitmap的使用

简介: 选中项目按右键添加-资源文件-WPF-窗口generate_bitmap.xml文件内容为

选中项目按右键添加-资源文件-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效果
image

目录
相关文章
|
数据可视化 C#
WPF 中的 NameScope
原文:WPF 中的 NameScope 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:http://blog.csdn.net/wpwalter/),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
972 0
|
C# 图形学 数据格式
WPF中的3D Wireframe
原文:WPF中的3D Wireframe WPF不支持画三维线,但开发人员提供了ScreenSpaceLines3D 类用于实现这个功能。
1289 0
|
C# 前端开发
WPF 小技巧
原文:WPF 小技巧 在使用mvvm模式开发时,对于Command的绑定是一件很伤脑筋的事情,尽管有强大的Blend类库支持: xmlns:Custom="http://www.galasoft.ch/mvvmlight"xmlns:i="http://schemas.
749 0
|
数据建模 C#
WPF InkCanvas 毛笔效果
原文:WPF InkCanvas 毛笔效果 1、先来看看InkCanvas的一般用法:                                                                                 2、自定义InkCanvas,实现毛笔效果...
1192 0
|
C#
wpf简单的绘图板
原文:wpf简单的绘图板 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a771948524/article/details/9323121 ...
856 0
|
算法 C#
WPF 实现水纹效果
原文:WPF 实现水纹效果 鼠标滑过产生水纹,效果图如下:     XMAL就放置了一个img标签   后台主要代码 窗体加载: private void Window_Loaded(object s...
1494 0
|
C#
WPF党旗和国徽!
原文:WPF党旗和国徽! 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangyisen0713/article/details/18087007 ...
616 0
|
C#
浅谈WPF的VisualBrush
原文:浅谈WPF的VisualBrush     首先看看VisualBrush的解释,msdn上面的解释是使用 Visual 绘制区域,那么我们再来看看什么是Visual呢?官方的解释是:获取或设置画笔的内容,Visual 是直接继承自DependencyObject,UIElement也是直接继...
1885 0
|
C# 测试技术
[WPF] PerformClick ?
原文:[WPF] PerformClick ?                                       [WPF] PerformClick ?                                                   周银辉   WPF没有提供这个方法,还真是让人觉得有些讨厌啊。
1038 0