• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    关于WPF WriteableBitmap类直接操作像素点的问题

    发布者: 天下网吧 | 发布时间: 2025-6-18 08:11| 查看数: 77| 评论数: 0|帖子模式

    WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分。它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交互用户图形界面。
    还是话不多说,直接上码:
    1.新建WpfApp应用程序
    2.MainWindow.xaml文件代码如下:
    1. <Window x:Class="WpfApp1.MainWindow"
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6.         xmlns:local="clr-namespace:WpfApp1"
    7.         mc:Ignorable="d"
    8.         Title="MainWindow" Height="450" Width="800">
    9.     <Grid>
    10.         <Grid.RowDefinitions>
    11.             <RowDefinition Height="*"></RowDefinition>
    12.             <RowDefinition Height="10*" ></RowDefinition>
    13.         </Grid.RowDefinitions>

    14.         <Button Name="button" Grid.Row="0" HorizontalAlignment="Center" Content="generate_bitmap" MinWidth="120"  MinHeight="30" Click="Button_Click"></Button>
    15.         <Grid x:Name="imgGrid" Grid.Row="1">
    16.             <Viewbox>
    17.                 <Image x:Name="img"  Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
    18.                        Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"
    19.                         Source="{Binding CtrlImage, IsAsync=True}"
    20.                       Stretch="None" />
    21.             </Viewbox>
    22.         </Grid>
    23.     </Grid>
    24. </Window>
    复制代码
    3.MainWindow.xaml.cs文件代码如下:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Globalization;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Windows;
    7. using System.Windows.Controls;
    8. using System.Windows.Data;
    9. using System.Windows.Documents;
    10. using System.Windows.Input;
    11. using System.Windows.Media;
    12. using System.Windows.Media.Imaging;
    13. using System.Windows.Navigation;
    14. using System.Windows.Shapes;
    15. using System.Drawing;
    16. using System.Drawing.Drawing2D;

    17. namespace WpfApp1
    18. {
    19.     /// <summary>
    20.     /// Interaction logic for MainWindow.xaml
    21.     /// </summary>
    22.     public partial class MainWindow : Window
    23.     {
    24.         public MainWindow()
    25.         {
    26.             InitializeComponent();
    27.         }
    28.         public void Button_Click(object sender, RoutedEventArgs e)
    29.             WriteableBitmap wb = new WriteableBitmap((int)imgGrid.ActualWidth, (int)imgGrid.ActualHeight, 96, 96, PixelFormats.Pbgra32, null);
    30.             wb.Lock();
    31.             Bitmap backBitmap = new Bitmap((int)imgGrid.ActualWidth, (int)imgGrid.ActualHeight, wb.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, wb.BackBuffer);
    32.             Int32Rect rect = new Int32Rect(0, 0, (int)imgGrid.ActualWidth, (int)imgGrid.ActualHeight);
    33.             byte[] pixels = new byte[(int)imgGrid.ActualWidth * (int)imgGrid.ActualHeight * wb.Format.BitsPerPixel / 8];
    34.             Random rand = new Random();
    35.             for (int y = 0; y < wb.PixelHeight; y++)
    36.             {
    37.                 for (int x = 0; x < wb.PixelWidth; x++)
    38.                 {
    39.                     int alpha = 0;
    40.                     int red = 0;
    41.                     int green = 0;
    42.                     int blue = 0;
    43.                     if ((x % 5 == 0) || (y % 7 == 0))
    44.                     {
    45.                         red = (int)((double)y / wb.PixelHeight * 255);
    46.                         green = rand.Next(100, 255);
    47.                         blue = (int)((double)x / wb.PixelWidth * 255);
    48.                         alpha = 255;
    49.                     }
    50.                     else
    51.                         red = (int)((double)x / wb.PixelWidth * 255);
    52.                         blue = (int)((double)y / wb.PixelHeight * 255);
    53.                         alpha = 50;
    54.                     int pixeloffset = (x + y * wb.PixelWidth) * wb.Format.BitsPerPixel / 8;
    55.                     pixels[pixeloffset] = (byte)blue;
    56.                     pixels[pixeloffset + 1] = (byte)green;
    57.                     pixels[pixeloffset + 2] = (byte)red;
    58.                     pixels[pixeloffset + 3] = (byte)alpha;
    59.                 }
    60.                 int stride = (wb.PixelWidth * wb.Format.BitsPerPixel) / 8;
    61.                 wb.WritePixels(rect, pixels, stride, 0);
    62.             }
    63.             wb.Unlock();
    64.             backBitmap.Dispose();
    65.             backBitmap = null;
    66.             img.Source = wb;
    67.     }
    68. }
    复制代码
      效果如下:

    到此这篇关于C#中WPF WriteableBitmap类直接操作像素点的文章就介绍到这了,更多相关WPF WriteableBitmap类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/article/235849.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表