当前位置: 代码网 > it编程>编程语言>Asp > 关于WPF WriteableBitmap类直接操作像素点的问题

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

2024年05月15日 Asp 我要评论
wpf(windows presentation foundation)是微软推出的基于windows 的用户界面框架,属于.net framework 3.0的一部分。它提供了统一的编程模型、语言和

wpf(windows presentation foundation)是微软推出的基于windows 的用户界面框架,属于.net framework 3.0的一部分。它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交互用户图形界面。

还是话不多说,直接上码:

1.新建wpfapp应用程序

2.mainwindow.xaml文件代码如下:

<window x:class="wpfapp1.mainwindow"
        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="mainwindow" height="450" width="800">
    <grid>
        <grid.rowdefinitions>
            <rowdefinition height="*"></rowdefinition>
            <rowdefinition height="10*" ></rowdefinition>
        </grid.rowdefinitions>
 
        <button name="button" grid.row="0" horizontalalignment="center" content="generate_bitmap" minwidth="120"  minheight="30" click="button_click"></button>
        <grid x:name="imggrid" grid.row="1">
            <viewbox>
                <image x:name="img"  width="{binding relativesource={relativesource self}, path=source.pixelwidth}"
                       height="{binding relativesource={relativesource self}, path=source.pixelheight}"
                        source="{binding ctrlimage, isasync=true}"
                      stretch="none" />
            </viewbox>
        </grid>
    </grid>
</window>

3.mainwindow.xaml.cs文件代码如下:

using system;
using system.collections.generic;
using system.globalization;
using system.linq;
using system.text;
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.navigation;
using system.windows.shapes;
using system.drawing;
using system.drawing.drawing2d;
 
namespace wpfapp1
{
    /// <summary>
    /// interaction logic for mainwindow.xaml
    /// </summary>
    public partial class mainwindow : window
    {
        public mainwindow()
        {
            initializecomponent();
        }
        public void button_click(object sender, routedeventargs e)
            writeablebitmap wb = new writeablebitmap((int)imggrid.actualwidth, (int)imggrid.actualheight, 96, 96, pixelformats.pbgra32, null);
            wb.lock();
            bitmap backbitmap = new bitmap((int)imggrid.actualwidth, (int)imggrid.actualheight, wb.backbufferstride, system.drawing.imaging.pixelformat.format32bppargb, wb.backbuffer);
            int32rect rect = new int32rect(0, 0, (int)imggrid.actualwidth, (int)imggrid.actualheight);
            byte[] pixels = new byte[(int)imggrid.actualwidth * (int)imggrid.actualheight * 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;
                    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);
                        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);
            }
            wb.unlock();
            backbitmap.dispose();
            backbitmap = null;
            img.source = wb;
    }
}

  效果如下:

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com