如何用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;
} }
}
}

效果如下:

最新文章

  1. Objective-c快速入门
  2. ASP.NET中获取Repeater模板列中LinkButton按钮事件中获取ID等
  3. AngularJs之Scope作用域
  4. java回调初步学习
  5. 用Python写爬虫爬取58同城二手交易数据
  6. Delphi如何处理不同类型的文件
  7. urlrewriter的使用
  8. Tomcat - SSL操作大全
  9. Sass for循环中编译%时报错解决方案
  10. java多线程下载网络图片
  11. 字符串匹配算法之Rabin-Karp算法
  12. [经典] Best Time to Buy and Sell Stock
  13. Android退出程序
  14. 外部样式OL LI的定义 影响到了富文本框内的UL LI的定义,使用内部样式对其还原
  15. IDEA第八章----远程调试
  16. freemarker -include
  17. Kafka0.10.0安装配置
  18. python字符串、列表和文件对象总结
  19. mysql 优化之查询缓存(mysql8已经废弃这个功能)
  20. 【leetcode 简单】 第一百四十六题 最长和谐子序列

热门文章

  1. Redis查询当前库有多少个 key
  2. 小尝试一下 cocos2d
  3. C#并发编程
  4. js比较版本号
  5. C#之设计模式
  6. SQL Server存储过程创建和修改
  7. Linux学习之CentOS(二十)--CentOS6.4下修改MySQL编码方法
  8. JS,删除数据时候,多次确认后才删除。
  9. 编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)
  10. linux 卸载编译安装的软件