美文网首页高一一班程序员
10.WPF WriteableBitmap的简单应用

10.WPF WriteableBitmap的简单应用

作者: psmyfish | 来源:发表于2019-03-27 14:25 被阅读55次

新建一个WPF项目
reference:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.media.imaging.writeablebitmap?view=netframework-4.7.2
删除MainWindow
删除App.xaml
添加脚本 Program.cs

项目属性-》Application -》Startup object改为xxx.Program
Program脚本如下:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Input;

namespace WpfApp4
{
    class Program
    {
        static WriteableBitmap writeableBitmap;
        static Window w;
        static Image i;

        [STAThread]
        static void Main(string[] args)
        {
            i = new Image();
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor);
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased);

            w = new Window();
            w.Content = i;
            w.Show();

            writeableBitmap = new WriteableBitmap(
                (int)w.ActualWidth,
                (int)w.ActualHeight,
                96,
                96,
                PixelFormats.Bgr32,
                null);

            i.Source = writeableBitmap;

            i.Stretch = Stretch.None;
            i.HorizontalAlignment = HorizontalAlignment.Left;
            i.VerticalAlignment = VerticalAlignment.Top;

            i.MouseMove += new MouseEventHandler(i_MouseMove);
            i.MouseLeftButtonDown +=
                new MouseButtonEventHandler(i_MouseLeftButtonDown);
            i.MouseRightButtonDown +=
                new MouseButtonEventHandler(i_MouseRightButtonDown);

            w.MouseWheel += new MouseWheelEventHandler(w_MouseWheel);

            Application app = new Application();
            app.Run();
        }

        // The DrawPixel method updates the WriteableBitmap by using
        // unsafe code to write a pixel into the back buffer.
        static void DrawPixel(MouseEventArgs e)
        {
            int column = (int)e.GetPosition(i).X;
            int row = (int)e.GetPosition(i).Y;

            try
            {
                // Reserve the back buffer for updates.
                writeableBitmap.Lock();

                unsafe
                {
                    // Get a pointer to the back buffer.
                    int pBackBuffer = (int)writeableBitmap.BackBuffer;

                    // Find the address of the pixel to draw.
                    pBackBuffer += row * writeableBitmap.BackBufferStride;
                    pBackBuffer += column * 4;

                    // Compute the pixel's color.
                    int color_data = 255 << 16; // R
                    color_data |= 128 << 8;   // G
                    color_data |= 255 << 0;   // B

                    // Assign the color data to the pixel.
                    *((int*)pBackBuffer) = color_data;
                }

                // Specify the area of the bitmap that changed.
                writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
            }
            finally
            {
                // Release the back buffer and make it available for display.
                writeableBitmap.Unlock();
            }
        }

        static void ErasePixel(MouseEventArgs e)
        {
            byte[] ColorData = { 0, 0, 0, 0 }; // B G R

            Int32Rect rect = new Int32Rect(
                    (int)(e.GetPosition(i).X),
                    (int)(e.GetPosition(i).Y),
                    1,
                    1);

            writeableBitmap.WritePixels(rect, ColorData, 4, 0);
        }

        static void i_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ErasePixel(e);
        }

        static void i_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DrawPixel(e);
        }

        static void i_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DrawPixel(e);
            }
            else if (e.RightButton == MouseButtonState.Pressed)
            {
                ErasePixel(e);
            }
        }

        static void w_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            System.Windows.Media.Matrix m = i.RenderTransform.Value;

            if (e.Delta > 0)
            {
                m.ScaleAt(
                    1.5,
                    1.5,
                    e.GetPosition(w).X,
                    e.GetPosition(w).Y);
            }
            else
            {
                m.ScaleAt(
                    1.0 / 1.5,
                    1.0 / 1.5,
                    e.GetPosition(w).X,
                    e.GetPosition(w).Y);
            }

            i.RenderTransform = new MatrixTransform(m);
        }
    }
}

相关文章

  • 10.WPF WriteableBitmap的简单应用

    新建一个WPF项目reference:https://docs.microsoft.com/zh-cn/dotne...

  • 简单应用

    统计a.sh文件中hello出现的频率 输出a.sh文件中hello出现的行数 输出文档的前n行记录 输出文档后n...

  • WKWebView的简单应用

    苹果公司在2014 WWDC上发布了iOS8,同时引入了WKWebView来替代传统的UIWebView。现在的产...

  • 函数的简单应用

    1.函数的简单应用 公共: str, int,float,bool,bytes,list,set,tuple,...

  • redux的简单应用

    1、安装 redux和react-redux: npm i redux react-redux --save-de...

  • VUEX的简单应用

  • EasyAR的简单应用

    为什么选EasyAR AR的广泛应用 AR(Augmented Reality)即增强现实,是一种实时地计算摄影机...

  • WebSocket的简单应用

    1、WebSocket简单介绍 WebSocket是html5规范中的一个部分,它借鉴了socket这种思想,为w...

  • CAShapeLayer的简单应用

    CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层CALayer的子类。你指定诸如颜色和线宽...

  • 盒子的简单应用

    内边距就是内边距到边框的距离。 padding :上 右 下 左 四边分别设置 padding:上 右 下 上下...

网友评论

    本文标题:10.WPF WriteableBitmap的简单应用

    本文链接:https://www.haomeiwen.com/subject/pgszvqtx.html