WPF 如何画一颗心

原文:WPF 如何画一颗心

如何用WPF画一个心。

MainWindow.xaml

<Window x:Class="Heart.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:Heart"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="66*"/>
            <RowDefinition Height="74*"/>
            <RowDefinition Height="77*"/>
            <RowDefinition Height="98*"/>
            <RowDefinition Height="69*"/>
            <RowDefinition Height="62*"/>
            <RowDefinition Height="72*"/>
            <RowDefinition Height="52*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="134*"/>
            <ColumnDefinition Width="525*"/>
            <ColumnDefinition Width="134*"/>
        </Grid.ColumnDefinitions>
        <Button Content="画心" Grid.Column="0" Grid.Row="1" Click="ButtonStart_Click" Width="100"></Button>
        <Canvas x:Name="canvas_Shape" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="500" Margin="12,0" Grid.Column="1" Grid.RowSpan="8"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;

namespace Heart
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            imageList = new List<System.Windows.Controls.Image>();
            imageList1 = new List<System.Windows.Controls.Image>();
            CreateHeartLine(true);
            CreateHeartLine(false);

            m_iImageCount = imageList.Count;
        }
        private int maxStep = 50;
        private double radius;
        private double centerPt;
        private Bitmap m_Snow;
        private Bitmap m_Snow1;
        private int m_iImageCount = 0;

        private List<System.Windows.Controls.Image> imageList = null;
        private List<System.Windows.Controls.Image> imageList1 = null;
        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);
        private BitmapSource GetBitmapSource(Bitmap bitmap)
        {
            IntPtr inptr = bitmap.GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                inptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(inptr);
            return bitmapSource;
        }

        private void SetImageSoure(System.Windows.Controls.Image img , Bitmap mSnow)
        {
            BitmapSource bitmapSource = GetBitmapSource(mSnow);
            img.Source = bitmapSource;
        }
        private void CreateHeartLine(bool bShow)
        {
            centerPt = canvas_Shape.Width / 2;
            radius = canvas_Shape.Width / 6;
            for (int i = 0; i < maxStep; i++)
            {
                System.Windows.Controls.Image img = new System.Windows.Controls.Image();
                BitmapSource bitmapSource;

                if (bShow)
                {
                    bitmapSource  = GetBitmapSource(Snow);
                    img.Source = bitmapSource;
                    img.Visibility = Visibility.Hidden;
                    imageList.Add(img);
                }
                else
                {
                    bitmapSource = GetBitmapSource(Snow1);
                    img.Source = bitmapSource;
                    imageList1.Add(img);
                }
                double angle = 2 * Math.PI / maxStep * i;
                double r = 2 * radius * (1 - Math.Sin(angle));
                //桃形心
                double x = centerPt - 16 * (Math.Sin(angle) * Math.Sin(angle) * Math.Sin(angle)) * 10;//
                double y = centerPt - (13 * Math.Cos(angle) - 5 * Math.Cos(2 * angle) - 2 * Math.Cos(3 * angle) - Math.Cos(4 * angle)) * 10;//
                Canvas.SetLeft(img, x);
                Canvas.SetTop(img, y);
                canvas_Shape.Children.Add(img);
            }
        }

        private Bitmap Snow
        {
            get
            {
                if (m_Snow == null)
                {
                    m_Snow = new Bitmap(32, 32);
                    using (Graphics g = Graphics.FromImage(m_Snow))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.Clear(System.Drawing.Color.Transparent);
                        g.TranslateTransform(16, 16, MatrixOrder.Append);
                        System.Drawing.Color black = System.Drawing.Color.FromArgb(255, 255, 255);
                        System.Drawing.Color white = System.Drawing.Color.FromArgb(255, 0, 255);
                        DrawSnow(g, new SolidBrush(black), new System.Drawing.Pen(black, 3f));
                        DrawSnow(g, new SolidBrush(white), new System.Drawing.Pen(white, 2f));
                        g.Save();
                    }
                }
                return m_Snow;
            }
        }
        private Bitmap Snow1
        {
            get
            {
                if (m_Snow1 == null)
                {
                    m_Snow1 = new Bitmap(32, 32);
                    using (Graphics g = Graphics.FromImage(m_Snow1))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.Clear(System.Drawing.Color.Transparent);
                        g.TranslateTransform(16, 16, MatrixOrder.Append);
                        System.Drawing.Color black = System.Drawing.Color.FromArgb(255, 255, 255);
                        System.Drawing.Color white = System.Drawing.Color.FromArgb(254, 194, 253);
                        DrawSnow(g, new SolidBrush(black), new System.Drawing.Pen(black, 3f));
                        DrawSnow(g, new SolidBrush(white), new System.Drawing.Pen(white, 2f));
                        g.Save();
                    }
                }
                return m_Snow1;
            }
        }
        private static void DrawSnow(Graphics g, System.Drawing.Brush b, System.Drawing.Pen p)
        {
            const int a = 6;
            const int a2 = a + 2;
            const int r = 2;
            g.DrawLine(p, -a, -a, +a, +a);
            g.DrawLine(p, -a, +a, +a, -a);
            g.DrawLine(p, -a2, 0, +a2, 0);
            g.DrawLine(p, 0, -a2, 0, +a2);
            g.FillEllipse(b, -r, -r, r * 2, r * 2);
        }

        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(ShowImageList);

            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();

        }
        private void ShowImageList()
        {
            while (true)
            {
                for (int i = 0; i < imageList.Count; i++)
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        ShowImageIndex(i);
                    }));
                    Thread.Sleep(100);
               }

            }
        }

        private void ShowImageIndex(int index)
        {

            if (imageList1[index].Visibility == Visibility.Visible)
            {
                imageList1[index].Visibility = Visibility.Hidden;
                imageList[index].Visibility = Visibility.Visible;
            }
            else
            {
                imageList1[index].Visibility = Visibility.Visible;
                imageList[index].Visibility = Visibility.Hidden;
            }

        }
    }
}

效果如下:

原文地址:https://www.cnblogs.com/lonelyxmas/p/9706961.html

时间: 2024-08-30 06:55:15

WPF 如何画一颗心的相关文章

从零开始学习PYTHON3讲义(十二)画一颗心送给你

(内容需要,本讲使用了大量在线公式,如果因为转帖网站不支持公式无法显示的情况,欢迎访问原始博客.) <从零开始PYTHON3>第十二讲 上一节课我们主要讲解了数值计算和符号计算.数值计算的结果,很常用的目的之一就是用于绘制图像,从图像中寻找公式的更多内在规律. Python科学绘图 科学绘图是计算机图形学的一个重要分支.同其它绘图方式相比,更简单易用,能让使用者把工作的主要精力集注在公式和算法上而不是绘图本身.此外科学绘图的工具包普遍精度更高,数据.图的对应关系准确,从而保证基于图的研究工作顺

WPF中画虚线

原文:WPF中画虚线 在WPF中,画线的方法十分简单,只要声明一个Line然后添加到指定的位置就可以了,但Line并不仅仅只能画一条直线,还可以对直线进行修饰. 1.Line.StrokeDashArray属性 StrokeDashArray是一个双精度字符串,指示用于勾勒形状轮廓的虚线和间距的样式. 2.Line.StrokeDashCap属性 获取或设置一个 PenLineCap 枚举值,该值指定如何绘制虚线的两端. 3.Line.StrokeEndLineCap和Line.StrokeSt

WPF设计の画刷(Brush)

原文:WPF设计の画刷(Brush) 一.什么是画刷 画刷是是一种渲染方式,用于填充图形形状,如矩形.椭圆.扇形.多边形和封闭路径.在GDI+中,画刷分为以下几种:SolidBrush,TextureBrush,HatchBrush,LinearGradientBrush和PathGradientBrush.在层次关系上,它们都位于System.Drawing空间下,继承自System.Drawing.Brush类. 画刷主要分为三大类: 1.SolidBrush(实心画刷)我们最常用 实心画刷

WPF Path 画箭头

原文:WPF Path 画箭头 代码: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height=

就算会用python画颗心,可你依然还是只单身狗

:) 标题是开玩笑的,千万别认真. 随着AI的飞速发展,有志于此行的码农也是急剧的增加,带来的就是大家对算法.数学的兴趣也格外升高. 本文的来历是这样,今天某老同事在朋友圈发了一张屏拍,求公式. 看了一下还是难度不大,上半部分基本是两个半圆,下半部分是两个旋转了的反余弦函数. 不过我的数学也比较渣,看到这个步骤后面也就倒腾不清了,不过到这种程度在互联网上搜一搜找到答案还是不难的,很快就找到了正确的公式(以y=0为界限,肯定是需要两组解): $$ y = \sqrt{1-(\left| x \ri

用Python画一颗特别的心送给她

import numpy as np import matplotlib.pyplot as plt x_coords = np.linspace(-100, 100, 500) y_coords = np.linspace(-100, 100, 500) points = [] for y in y_coords: for x in x_coords: if ((x * 0.03) ** 2 + (y * 0.03) ** 2 - 1) ** 3 - (x * 0.03) ** 2 * (y

基于微博数据用 Python 打造一颗“心”

一年一度的虐狗节刚过去不久,朋友圈各种晒,晒自拍,晒娃,晒美食,秀恩爱的.程序员在晒什么,程序员在加班.但是礼物还是少不了的,送什么好?作为程序员,我准备了一份特别的礼物,用以往发的微博数据打造一颗“爱心”,我想她一定会感动得哭了吧.哈哈 准备工作 有了想法之后就开始行动了,自然最先想到的就是用 Python 了,大体思路就是把微博数据爬下来,数据经过清洗加工后再进行分词处理,处理后的数据交给词云工具,配合科学计算工具和绘图工具制作成图像出来,涉及到的工具包有: requests 用于网络请求爬

编程模拟自然(一):如何画一颗静态树

序 万物初始之前,宇宙是无边无际混沌的黑暗,只有元之灵穿行其间.元对这无边的黑暗十分不满,就轻轻一敲键盘,说:“要有光”,于是世间就有了光.元称“光”为“昼”,称“黑暗”为“夜”.亮光隐去,黑暗重临,从此,世间就有了昼与夜的交替.这是元创世的第一天. 这个二次元世界诞生之初伴有一只程序猿,Ta的名字叫元. 第一章 二叉树必须是最好的树! 元就是这样想的,为了表达对这种数据结构的崇拜之情,决定在这个世界造出一颗真实的树,一开始的构想是这样的 “正所谓树极生三干,三干生六枝,六枝生十二叶.” 然而这

一支笔,一颗心

我们上大学的目的到底是什么,或者说我们来到这个世界的原因到底是什么?我们能为这个世界带来什么?今天我的兄弟突然告诉我他要退学,面对如此语境竟让我一时语塞,我想可能在泥潭里摸爬滚打久了的话,也会失去自己原有的本色吧--,但是听了他的一番诉说之后,让我明白我之前所认识到的人生的虚拟场景竟然在这一刻浮现了,的确,我们并不属于任何人,因为我们属于这个世界,我们拥有这个世界,我们愿意为他肝脑涂地,愿意为她牺牲一切,这可不是一种狭隘的主见,这也不是一种自私的行为,这只是世界对我们所要求的反馈,我们来到某种境