如何用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="" Width="">
<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="" Grid.Row="" Click="ButtonStart_Click" Width=""></Button>
<Canvas x:Name="canvas_Shape" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="" Margin="12,0" Grid.Column="" Grid.RowSpan=""/>
</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 = ;
private double radius;
private double centerPt;
private Bitmap m_Snow;
private Bitmap m_Snow1;
private int m_iImageCount = ; 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 / ;
radius = canvas_Shape.Width / ;
for (int i = ; 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 = * Math.PI / maxStep * i;
double r = * radius * ( - Math.Sin(angle));
//桃形心
double x = centerPt - * (Math.Sin(angle) * Math.Sin(angle) * Math.Sin(angle)) * ;//
double y = centerPt - ( * Math.Cos(angle) - * Math.Cos( * angle) - * Math.Cos( * angle) - Math.Cos( * angle)) * ;//
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(, );
using (Graphics g = Graphics.FromImage(m_Snow))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(System.Drawing.Color.Transparent);
g.TranslateTransform(, , MatrixOrder.Append);
System.Drawing.Color black = System.Drawing.Color.FromArgb(, , );
System.Drawing.Color white = System.Drawing.Color.FromArgb(, , );
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(, );
using (Graphics g = Graphics.FromImage(m_Snow1))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(System.Drawing.Color.Transparent);
g.TranslateTransform(, , MatrixOrder.Append);
System.Drawing.Color black = System.Drawing.Color.FromArgb(, , );
System.Drawing.Color white = System.Drawing.Color.FromArgb(, , );
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 = ;
const int a2 = a + ;
const int r = ;
g.DrawLine(p, -a, -a, +a, +a);
g.DrawLine(p, -a, +a, +a, -a);
g.DrawLine(p, -a2, , +a2, );
g.DrawLine(p, , -a2, , +a2);
g.FillEllipse(b, -r, -r, r * , r * );
} 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 = ; i < imageList.Count; i++)
{
this.Dispatcher.Invoke((Action)(() =>
{
ShowImageIndex(i);
}));
Thread.Sleep();
} }
} 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;
} }
}
}

效果如下:

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

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

    (内容需要,本讲使用了大量在线公式,如果因为转帖网站不支持公式无法显示的情况,欢迎访问原始博客.) <从零开始PYTHON3>第十二讲 上一节课我们主要讲解了数值计算和符号计算.数值计算的 ...

  2. WPF中画虚线

    原文:WPF中画虚线 在WPF中,画线的方法十分简单,只要声明一个Line然后添加到指定的位置就可以了,但Line并不仅仅只能画一条直线,还可以对直线进行修饰. 1.Line.StrokeDashAr ...

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

    :) 标题是开玩笑的,千万别认真. 随着AI的飞速发展,有志于此行的码农也是急剧的增加,带来的就是大家对算法.数学的兴趣也格外升高. 本文的来历是这样,今天某老同事在朋友圈发了一张屏拍,求公式. 看了 ...

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

    import numpy as np import matplotlib.pyplot as plt x_coords = np.linspace(-100, 100, 500) y_coords = ...

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

    一年一度的虐狗节刚过去不久,朋友圈各种晒,晒自拍,晒娃,晒美食,秀恩爱的.程序员在晒什么,程序员在加班.但是礼物还是少不了的,送什么好?作为程序员,我准备了一份特别的礼物,用以往发的微博数据打造一颗“ ...

  6. WPF 如何画出1像素的线

    如何有人告诉你,请你画出1像素的线,是不是觉得很简单,实际上在 WPF 上还是比较难的. 本文告诉大家,如何让画出的线不模糊 画出线的第一个方法,创建一个 Canvas ,添加一个线 界面代码 < ...

  7. 利用python基于微博数据打造一颗“心”

    一年一度的虐狗节将至,朋友圈各种晒,晒自拍,晒娃,晒美食,秀恩爱的.程序员在晒什么,程序员在加班.但是礼物还是少不了的,送什么好?作为程序员,我准备了一份特别的礼物,用以往发的微博数据打造一颗&quo ...

  8. 用Sklearn画一颗决策树

    小伙伴们大家好~o( ̄▽ ̄)ブ,首先声明一下,我的开发环境是Jupyter lab,所用的库和版本大家参考: Python 3.7.1(你的版本至少要3.4以上 Scikit-learn 0.20.0 ...

  9. WPF设计の画刷(Brush)

    一.什么是画刷 画刷是是一种渲染方式,用于填充图形形状,如矩形.椭圆.扇形.多边形和封闭路径.在GDI+中,画刷分为以下几种:SolidBrush,TextureBrush,HatchBrush,Li ...

随机推荐

  1. 源码编译安装postgresql

    依赖的库:libreadline-dev,zlib1g-dev 安装:下载解压源码包,然后 ./configure,make,make install即可.. 注意不能在root账户下跑server, ...

  2. C#基础练习

    1.冒泡排序 namespace _0 { class Program { public static int[] BubbleSort(int[] arr) { ; i < arr.Lengt ...

  3. java中HashMap重要性质和优化总结

    原文: http://www.cnblogs.com/junyuhuang/p/4519257.html

  4. HTML之JS学习

    提示篇 function fun(){ var is = confirm('选择对话框');/*确定取消对话框*/ if(is == true){ document.write('真');/*网页输出 ...

  5. 基于fastweixin的微信开发环境搭建(一)

    由于公司业务需要,开发微信版本,才开始接触微信公众平台.在github折腾了几天,试过好几个微信sdk,最终选择fastweixin.个人觉得这个框架还是值得使用的,使用也简单.那么问题来了,很多人想 ...

  6. apache_commons 之 双向Map DualHashBidiMap (使用及源码)

    在项目当中,经常出现需要根据Key值获取value:而且要求根据value获取key值,其实在commons-collections包中已经提供了此集合类.就是DualHashBidiMap类. (官 ...

  7. SCI英文论文写作- Latex 进阶

    SCI英文论文写作- Latex 进阶   1.设置行间距的方法: %\setlength{\baselineskip}{15pt} \renewcommand{\baselinestretch}{1 ...

  8. 初识pipeline

    1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...

  9. 强有力的Linux历史命令 你还记得几个

    列出所有出现到的命令:(所有一下信息都可以通过man history得到,而且还更多) history:列出历史中执行过的命令(-c清除所有的命令历史) !N:执行编号为N的历史命令 !-N:执行倒数 ...

  10. REDHAT一总复习1 cups 打印服务配置

    停止cups服务 $ sudo systemctl stop cups $ sudo systemctl status cups 配置cups服务,使其不在系统启动时启动 $ sudo systemc ...