当前位置: 首页 > news >正文

C# Avalonia 13- MoreDrawing - GenerateBitmap

GenerateBitmap.axaml代码

<Window xmlns="https://github.com/avaloniaui"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"Width="460" Height="472"x:Class="AvaloniaUI.GenerateBitmap"Title="GenerateBitmap"><Grid RowDefinitions="auto,*"><Button Content="Button" Grid.Row="1" Height="81" HorizontalAlignment="Left" Margin="106,90,0,0" Name="button1" VerticalAlignment="Top" Width="193" /><Button Content="Generate Bitmap" Margin="5" Padding="10" Click="cmdGenerate_Click" HorizontalAlignment="Center"></Button><Image Grid.Row="1" Name="img" Margin="5" Width="400" Height="300" IsHitTestVisible="False"></Image></Grid>
</Window>

GenerateBitmap.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using System;
using System.Runtime.InteropServices;namespace AvaloniaUI;public partial class GenerateBitmap : Window
{public GenerateBitmap(){InitializeComponent();}private void cmdGenerate_Click(object? sender, RoutedEventArgs e){// 确保图像尺寸有效if (img.Width <= 0 || img.Height <= 0) return;// 创建 WriteableBitmapvar size = new PixelSize((int)img.Width, (int)img.Height);var dpi = new Vector(96, 96);var wb = new WriteableBitmap(size, dpi, PixelFormat.Bgra8888, AlphaFormat.Premul);// 使用 Lock() 获取帧缓冲区using (var buffer = wb.Lock()){int bytesPerPixel = PixelFormat.Bgra8888.BitsPerPixel / 8; // BGRA8888 格式每个像素4字节(32位)int stride = buffer.RowBytes;Random rand = new Random();// 创建像素数组byte[] pixels = new byte[stride * size.Height];for (int y = 0; y < size.Height; y++){for (int x = 0; x < size.Width; x++){int alpha, red, green, blue;if ((x % 5 == 0) || (y % 7 == 0)){red = (int)((double)y / size.Height * 255);green = rand.Next(100, 255);blue = (int)((double)x / size.Width * 255);alpha = 255;}else{red = (int)((double)x / size.Width * 255);green = rand.Next(100, 255);blue = (int)((double)y / size.Height * 255);alpha = 50;}int pixelOffset = y * stride + x * bytesPerPixel;pixels[pixelOffset] = (byte)blue;     // Bpixels[pixelOffset + 1] = (byte)green; // Gpixels[pixelOffset + 2] = (byte)red;   // Rpixels[pixelOffset + 3] = (byte)alpha; // A}}// 使用 Marshal.Copy 将数据复制到缓冲区Marshal.Copy(pixels, 0, buffer.Address, pixels.Length);}img.Source = wb;}
}

运行效果

image

 

http://www.wxhsa.cn/company.asp?id=5350

相关文章:

  • Flutter个性化主题系统:Material Design 3的深度定制
  • Typescript中闭包的原理
  • IvorySQL 4.6:DocumentDB+FerretDB 实现 MongoDB 兼容部署指南
  • 在Xilinx Vitis中创建并使用静态库
  • Go使用cyclicbarrier示例
  • 做题记录2
  • 剑指offer-30、连续⼦数组的最⼤和
  • ITK-SNAP 安装
  • Morpheus 审计报告分享3:StETH 的精度丢失转账机制
  • 小区物业的智慧:轻松图解JVM垃圾回收的奥秘
  • SPI 总线概述及嵌入式 Linux 从属 SPI 设备驱动程序开发(第二部分,实践) - 教程
  • 详细介绍:idea2025创建第一个项目
  • CUDA多版本安装切换(转链接自用)
  • 社交交友源码:功能剖析、盈利探索与绿色运营策略
  • 权变与权力异化,是斗争的根源,超越自我,良性循环
  • 元推理AGI,是人类文明的结晶,超越爱因斯坦相对论,是文明进步的必然
  • PLC结构化文本设计模式——原型模式(Prototype Pattern)
  • 【一步步开发AI运动APP】十二、自定义扩展新运动项目1
  • 【Linux】人事档案——用户及组管理 - 详解
  • 试试这个AI邪修方法,让你刷推特时间节省80%
  • [数据结构——lesson10.2堆排序以及TopK障碍]
  • 终端里跑图形应用「GitHub 热点速览」
  • trl ppo
  • PHP-FPM 深度调优指南 告别 502 错误,让你的 PHP 应用飞起来
  • RAG系统大脑调教指南:模型选择、提示设计与质量控保一本通
  • 智驾终局:VLA与WA的“强脑”之争
  • 微软2018年第四季度顶级漏洞赏金猎人榜单揭晓
  • 能源汽车智能线控底盘
  • Linux中的LED子专业的系统
  • DP 凸性优化:wqs 二分