如何用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. mui 动态加载数据出现的问题处理 (silder )

    mui-slider 问题:动态给mui的图片轮播添加图片,轮播不滚动. 解决:最后把滚动轮播图片的mui(".mui-slider").slider({interval: 300 ...

  2. redis 操作string 的测试

    1>set  set name zhangsan :OK get name: zhangsan set name lisi:OK get name: lisi   2> setnx 如果存 ...

  3. 个人对B/S项目的一些理解(二)

    以下是我自工作以来,结合对C/S项目的认知,对B/S项目的一些理解. 如有不足或者错误,请各位指正.     ----数据处理的升级   在上面的描述中,大家也看到了,远古时期的程序员,其实也听不容易 ...

  4. web api7

  5. Java学习笔记-按值传递

    参数的值传递 实参必须与方法中次你故意的参数在次序和数量上匹配,在类型上兼容.类型兼容是指不需要经过显式的类型转换,实参的值就可以传递给形参.如将int型的实参值传递给double型形参. 当调用方法 ...

  6. getElementsByClassName简单实现

    function getElementsByClassName(node, className) { var aClassReg = className.split(' ').map(function ...

  7. mysql5.x升级到mysql5.7后导入之前数据库date出错的快速解决方法【mysql低版本数据导入到高版本出错的解决方法】

    mysql5.x升级至mysql5.7后导入之前数据库date出错,这是由于MySQL的sql_mode的影响,解决方法如下所示: [具体参考:mysql的sql_mode详解]修改mysql5.7的 ...

  8. COM 组件基础——GUID 和 接口

    一.前言 书接上回,话说在 doc(Word) 复合文件中,已经解决了保存 xls(Excel) 数据的问题了.那么,接下来又要解决另一个问题:当 WORD 程序读取复合文件,遇到了 xls 数据的时 ...

  9. 查询EBS中LOV的SQL语句

    1.帮助->关于:查找会话 SID : 507: 2.点一下LOV右边的三点,触发LOV事件: 3.运行如下代码段: DECLARE  l_sid NUMBER := :SID;BEGIN  F ...

  10. java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别

    File file = new File(".\\test.txt"); System.out.println(file.getPath()); System.out.printl ...