惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
T
Tenable Blog
D
DataBreaches.Net
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
V
Visual Studio Blog
B
Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
The Cloudflare Blog
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Martin Fowler
Martin Fowler
SecWiki News
SecWiki News
T
Threat Research - Cisco Blogs
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
Forbes - Security
Forbes - Security
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
O
OpenAI News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
S
Security @ Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
H
Help Net Security
Y
Y Combinator Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
量子位
U
Unit 42
S
SegmentFault 最新的问题
V
V2EX
D
Docker

博客园 - HotSky

Chrome启用CDP(chrome-devtools-protocol)进行远程操控 WPF 让ScrollViewer支持按住鼠标中键拖拽滚动内容 WPF 让ScrollViewer支持鼠标中键滚动缩放内容 Nodejs使用 nodejs中写sql需要用in时的写法 C#城市最短路径 C#Animation Sqlite PDF附录A: 内容流操作码 WPF FPS类 生命模拟 C# Sql帮助类,可扩展 Alpha混合 Bmp读写二值图 WPF支持任意快捷键+鼠标组合的绑定类 WPF阻止窗体被系统缩放,使用显示器DPI WPF DataGrid自动增长序号列 C#访问或修改私有类、函数、变量、属性 WPF一个简单的属性编辑控件
WPF WriteableBitmap通过GDI+绘制帮助类
HotSky · 2024-08-05 · via 博客园 - HotSky

代码:


    public class WriteableBitmapGraphic : IDisposable
    {
        public WriteableBitmap Source { get; private set; }
        public System.Drawing.Bitmap bitmap { get; private set; }
        public int DataLength { get; private set; }
        public Int32Rect SourceRect { get; private set; }
        public System.Drawing.Rectangle BitmapRect { get; private set; }
        public System.Drawing.Graphics Graphics { get; private set; }
        System.Drawing.Imaging.PixelFormat pixelFormat;
        bool flushed = false;
        public WriteableBitmapGraphic(WriteableBitmap writeableBitmap)
        {
            Source = writeableBitmap;
            DataLength = Source.BackBufferStride * Source.PixelHeight;
            SourceRect = new Int32Rect(0, 0, Source.PixelWidth, Source.PixelHeight);
            BitmapRect = DrawingUtil.ConvertRect(SourceRect);
            pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            bitmap = new System.Drawing.Bitmap(writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, pixelFormat);
            Graphics = System.Drawing.Graphics.FromImage(bitmap);
        }
        public void FillSource()
        {
            var block = bitmap.LockBits(BitmapRect, System.Drawing.Imaging.ImageLockMode.WriteOnly, pixelFormat);
            byte[] tempArr = new byte[DataLength];
            Marshal.Copy(Source.BackBuffer, tempArr, 0, DataLength);
            Marshal.Copy(tempArr, 0, block.Scan0, DataLength);
            bitmap.UnlockBits(block);
        }

        public void DrawImage(byte[] pixelArray, int width, int height, int x, int y)
        {
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, pixelFormat))
            {
                var block = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);
                Marshal.Copy(pixelArray, 0, block.Scan0, pixelArray.Length);
                bitmap.UnlockBits(block);
                DrawImage(bitmap, x, y);
            }
        }
        public void DrawImage(string imagePath, Point point)
        {
            using (var image = System.Drawing.Image.FromFile(imagePath))
                DrawImage(image, point);
        }
        public void DrawImage(System.Drawing.Image image, Point point)
        {
            DrawImage(image, (int)point.X, (int)point.Y);
        }
        public void DrawImage(System.Drawing.Image image, int x, int y)
        {
            Graphics.DrawImage(image, x, y);
        }


        public void ClearRandom()
        {
            Random random = new Random();
            var block = bitmap.LockBits(BitmapRect, System.Drawing.Imaging.ImageLockMode.ReadOnly, pixelFormat);
            int offset = 0;
            int n256 = 256;
            unsafe
            {
                byte* ptr = (byte*)block.Scan0;
                do
                {
                    ptr[offset++] = (byte)random.Next(n256);
                } while (offset < DataLength);
            }
            bitmap.UnlockBits(block);
        }

        public void Flush()
        {
            Graphics.Flush();
            var block = bitmap.LockBits(BitmapRect, System.Drawing.Imaging.ImageLockMode.ReadOnly, pixelFormat);
            byte[] tempArr = new byte[DataLength];
            Marshal.Copy(block.Scan0, tempArr, 0, DataLength);
            Source.Lock();
            Source.AddDirtyRect(SourceRect);
            Marshal.Copy(tempArr, 0, Source.BackBuffer, DataLength);
            Source.Unlock();
            bitmap.UnlockBits(block);
            flushed = true;
        }

        public void Dispose()
        {
            if(!flushed)
                Flush();
            Graphics?.Dispose();
            bitmap?.Dispose();
        }

        public void Clear(Color color)
        {
            Graphics.Clear(DrawingUtil.ConvertColor(color));
        }
    }
    public static class DrawingUtil
    {
        public static System.Drawing.SolidBrush CreateBrush(System.Windows.Media.Color color)
        {
            return new System.Drawing.SolidBrush(ConvertColor(color));
        }
        public static System.Drawing.Color ConvertColor(System.Windows.Media.Color color)
        {
            return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
        }
        public static System.Drawing.Rectangle ConvertRect(System.Windows.Int32Rect rect)
        {
            return new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
        }
    }

    public class CvGraphic : IDisposable
    {
        public WriteableBitmap Source { get; private set; }
        public OpenCvSharp.Mat SourceMat { get; private set; }
        public int Length { get; private set; }
        bool flushed = false;
        public CvGraphic(WriteableBitmap source)
        {
            Source = source;
            Length = source.PixelHeight * source.BackBufferStride;
            SourceMat = OpenCvSharp.Mat.FromPixelData(source.PixelHeight, source.PixelWidth, OpenCvSharp.MatType.CV_8UC4, source.BackBuffer);
        }
        public void Clear()
        {
            OpenCvSharp.Cv2.Randn(SourceMat, new OpenCvSharp.Vec4b(0, 0, 0, 0), new OpenCvSharp.Vec4b(0, 0, 0, 0));
        }

        public void Random()
        {
            OpenCvSharp.Cv2.Randn(SourceMat, new OpenCvSharp.Vec4b(0, 0, 0, 0), new OpenCvSharp.Vec4b(255, 255, 255, 255));
        }

        public void DrawImage(string filename, int x, int y)
        {
            if (x >= Source.PixelWidth || y >= Source.PixelHeight) return;
            using(OpenCvSharp.Mat mat = new OpenCvSharp.Mat(filename, OpenCvSharp.ImreadModes.Unchanged))
            {
                int width = Math.Min(SourceMat.Width - x, mat.Width);
                int height = Math.Min(SourceMat.Height - y, mat.Height);
                mat.CopyTo(SourceMat.SubMat(new OpenCvSharp.Range(x, x + width), new OpenCvSharp.Range(y, y + height)));
            }
        }

        public void Flush()
        {
            Source.WritePixels(new Int32Rect(0, 0, Source.PixelWidth, Source.PixelHeight), SourceMat.Data, Length, Source.BackBufferStride);
            flushed = true;
        }
        public void Dispose()
        {
            if(flushed)
                Flush();
            SourceMat?.Dispose();
        }
    }