WPF 晚上吃什么 制作gif动图,截图晚上吃什么

2023-12-22 19:18
文章标签 制作 截图 wpf 动图 gif 晚上

本文主要是介绍WPF 晚上吃什么 制作gif动图,截图晚上吃什么,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天看到了一个类似这样的gif,截图来决定晚上吃什么,于是动手自己写一个,生成如此Gif的程序

github:https://github.com/442040292/WhatForDinner 完整的项目、虽然是个简单的小程序

需要使用到这个dll: Gif.Components.dll

直接上代码:

主页:

<Window x:Class="WhatForDinner.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:WhatForDinner"mc:Ignorable="d" x:Name="uc"Title="MainWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="200"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Button  Content="解析并添加" Click="AddNewItem_Click" HorizontalAlignment="Left" Margin="37,133,0,0" VerticalAlignment="Top" Width="100" Height="41"/><TextBox x:Name="newItemString" HorizontalAlignment="Left" Height="115" TextWrapping="Wrap" Text="米线,麻辣烫,黄焖鸡,炒菜,饭团,麻辣香锅,拌面" VerticalAlignment="Top" Width="200"/><Button x:Name="button1" Content="创建" Click="Create_Click" HorizontalAlignment="Left" Margin="37,209,0,0" VerticalAlignment="Top" Width="100" Height="36"/><ScrollViewer Grid.Column="1" Grid.ColumnSpan="2"><ItemsControl ItemsSource="{Binding ItemSource,ElementName=uc}" ><ItemsControl.ItemTemplate><DataTemplate><Button Content="{Binding .}" Click="RemoveItem_Click"/></DataTemplate></ItemsControl.ItemTemplate><ItemsControl.ItemsPanel><ItemsPanelTemplate><WrapPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel></ItemsControl></ScrollViewer><TextBox x:Name="outputPath" HorizontalAlignment="Left" Height="67" Margin="0,352,0,0" TextWrapping="Wrap" Text="D://mygif.gif" VerticalAlignment="Top" Width="200"/></Grid>
</Window>

后面:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WhatForDinner
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();ItemSource = new ObservableCollection<string>();}public ObservableCollection<string> ItemSource{get { return (ObservableCollection<string>)GetValue(ItemSourceProperty); }set { SetValue(ItemSourceProperty, value); }}// Using a DependencyProperty as the backing store for ItemSource.  This enables animation, styling, binding, etc...public static readonly DependencyProperty ItemSourceProperty =DependencyProperty.Register("ItemSource", typeof(ObservableCollection<string>), typeof(MainWindow), new PropertyMetadata(null));private string SavePath = @"D://mygif.gif";private void Create_Click(object sender, RoutedEventArgs e){Helper.CreateGif(ItemSource, SavePath);outputPath.Text = SavePath;}private void AddNewItem_Click(object sender, RoutedEventArgs e){string newiRTEMS = newItemString.Text;if (!string.IsNullOrEmpty(newiRTEMS)){var items = newiRTEMS.Split(',', ',');foreach (var item in items){ItemSource.Add(item);}}var distList = ItemSource.Distinct().ToList();ItemSource = new ObservableCollection<string>(distList);}private void RemoveItem_Click(object sender, RoutedEventArgs e){var button = sender as Button;var content = button.Content.ToString();var removeItems = ItemSource.Where(x => x == content).ToList();foreach (var item in removeItems){if (ItemSource.Any(x => x == item)){ItemSource.Remove(item);}}}}
}

帮助类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;using Gif.Components;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;namespace WhatForDinner
{public class Helper{public static void CreateGif(IEnumerable<string> newItems, string savePath){if (newItems.Count() <= 0){return;}List<System.Drawing.Image> images = new List<System.Drawing.Image>();var maxWidth = newItems.Max(x => x.Length) * 3 * 14;var maxHeight = 100;foreach (var item in newItems){var image = FillRectangle(item, maxWidth, maxHeight);images.Add(image);}ConvertJpgToGif(images, savePath, 1, maxWidth, maxHeight);}/// <summary>/// 多张图片转成一张gif图片/// </summary>/// <param name="imageFilePaths">图片路径,放到一个数组里面</param>/// <param name="gifPath">生成的gif图片路径</param>/// <param name="time">每一帧图片间隔时间</param>/// <param name="w">生成的gif图片宽度</param>/// <param name="h">生成的gif图片高度</param>/// <returns></returns>public static bool ConvertJpgToGif(List<System.Drawing.Image> images, string gifPath, int time, int w, int h){try{AnimatedGifEncoder e = new AnimatedGifEncoder();e.Start(gifPath);e.SetDelay(time);//0:循环播放    -1:不循环播放e.SetRepeat(0);for (int i = 0, count = images.Count; i < count; i++){//e.AddFrame(Image.FromFile(Server.MapPath(imageFilePaths[i])));System.Drawing.Image img = images[i];//如果多张图片的高度和宽度都不一样,可以打开这个注释img = ReSetPicSize(img, w, h);e.AddFrame(img);}e.Finish();return true;}catch (Exception ex){return false;}}/// <summary>/// 重新调整图片的大小,使其满足要求/// </summary>/// <param name="image"></param>/// <param name="newWidth"></param>/// <param name="newHeight"></param>/// <returns></returns>public static Image ReSetPicSize(Image image, int newWidth, int newHeight){Bitmap bmp = new Bitmap(image);try{Bitmap b = new Bitmap(newWidth, newHeight);Graphics g = Graphics.FromImage(b);// 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newWidth, newHeight), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);g.Dispose();// return b;Image img = (Image)b;//  MessageBox.Show("Width"+img.Width.ToString() + "Height:" + img.Height.ToString());return img;}catch{return null;}}/// <summary>/// 在图片中绘制 矩形/// </summary>/// <param name="image"></param>/// <param name="x"></param>/// <param name="y"></param>/// <param name="width"></param>/// <param name="height"></param>public static System.Drawing.Image FillRectangle(string text, int rectWith, int rectHeight){Bitmap b = new Bitmap(rectWith, rectHeight);System.Drawing.Image image = b;//b.Save("aa.jpg", ImageFormat.Jpeg);Graphics graphics = Graphics.FromImage(image);graphics.FillRectangle(new SolidBrush(System.Drawing.Color.White), new RectangleF(0, 0, rectWith, rectWith));graphics.DrawString(text, new Font("微软雅黑", 14), new SolidBrush(System.Drawing.Color.Black), rectWith / 2, rectHeight / 2, new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });//红色的圈显示 点击位置graphics.Flush();graphics.Dispose();return image;}}
}

小技巧: 为了不附带dll 只用一个 exe 方便转发 使用直接 把dll嵌入到exe里面去了

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;namespace WhatForDinner
{/// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : Application{protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;}private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args){string[] embedDll = new string[] { "Gif.Components" };string resourceName = "WhatForDinner." + new AssemblyName(args.Name).Name + ".dll";using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)){if (stream != null){byte[] assemblyData = new byte[stream.Length];stream.Read(assemblyData, 0, assemblyData.Length);return Assembly.Load(assemblyData);}else{return null;}}}}
}

OK 再次提醒晚上吃啥   

这篇关于WPF 晚上吃什么 制作gif动图,截图晚上吃什么的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/525152

相关文章

用Microsoft.Extensions.Hosting 管理WPF项目.

首先引入必要的包: <ItemGroup><PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" /><PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /><PackageReference Include="Serilog

使用XmlPullParser制作BindView工具

在之前我写过了一个BindView的工具,之前使用的最要是正则表达的文本分析做的。最近,工作我认识了Android的XML解析,我又想起了这个问题。发现这个问题,其实用XmlPullParser更好解决。所以我重新写了这个工具。简单多了,而且不用格式化代码。 先分析一下如何写,简易思路如下 Created with Raphaël 2.1.0 输入文本路径 读取x

[分布式网络通讯框架]----ZooKeeper下载以及Linux环境下安装与单机模式部署(附带每一步截图)

首先进入apache官网 点击中间的see all Projects->Project List菜单项进入页面 找到zookeeper,进入 在Zookeeper主页的顶部点击菜单Project->Releases,进入Zookeeper发布版本信息页面,如下图: 找到需要下载的版本 进行下载既可,这里我已经下载过3.4.10,所以以下使用3.4.10进行演示其他的步骤。

Linux RedHat 利用 ISO镜像文件制作本地 yum源

优质博文:IT-BLOG-CN 【1】创建iso存放目录和挂载目录 [root@desktop ~]# cd /mnt/[root@desktop mnt]# mkdir cdrom 【2】将ISO镜像文件挂载到/mnt/cdrom文件夹下(前提你的CD/DVD中有你的ISO文件-安装时使用的镜像文件) mount /dev/cdrom /mnt/cdrom 【3】编辑/et

2023-2024 学年第二学期小学数学六年级期末质量检测模拟(制作:王胤皓)(90分钟)

word效果预览: 一、我会填 1. 1.\hspace{0.5em} 1. 一个多位数,亿位上是次小的素数,千位上是最小的质数的立方,十万位是 10 10 10 和 15 15 15 的最大公约数,万位是最小的合数,十位上的数既不是质数也不是合数,这个数是 ( \hspace{4em} ),约等于 ( \hspace{1em} ) 万 2. 2.\hspace{0.5em} 2.

Flask中制作博客首页的分类功能(二)

在Flask中制作博客首页的分类功能(一)的基础上,继续下面的教程。 发布文章的时候会为文章添加一些标签(tag),这些tag需要和数据库中Category表的tag进行比较,如果已经存在该tag,那么需要将新发表文章的tag与已存在的表格进行对应,如果不存在则要新建一个category表。 首先在python shell中执行操作。 from sql_test import db, Pos

制作微信小程序“飞翔的小鸟”

微信小程序为开发者提供了一个强大的平台,可以快速创建各种有趣的应用。在这篇博客中,我们将介绍如何制作一个简单的微信小程序——“飞翔的小鸟”。 项目介绍 “飞翔的小鸟”是一款基于微信小程序的小游戏,玩家需要控制一只小鸟在障碍物之间飞行,避免撞到柱子。游戏难度逐渐增加,挑战玩家的反应速度和操作技巧。 准备工作 注册微信小程序账号:首先,你需要在微信公众平台注册一个小程序账号。 安

应届毕业之本科简历制作

因为毕设以及编制岗位面试,最近好久没有更新了,刚好有同学问如何制作简历,我就准备将我自己制作简历的流程分享给各位,到此也算是一个小的结束,拿了工科学位证书毕业去做🐂🐎了。 简历主要包含内容 基本信息 基本信息需要可以分为必要的信息和可选的信息。必要的信息包括 姓名 、 联系方式(手机号 + 邮箱) ,可 选的信息包括城市、求职岗位、照片、城市、现工作地点、期望工作地点、性别。

Python使用tkinter制作无边框透明时钟源码讲解(tkinter如何实现窗口无边框透明)

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 文章内容 📒📝 导入必要的库📝 创建主窗口🎯 去掉窗口边框🎯 设置窗口透明度🎯 允许窗口背景透明🎯 设置窗口背景颜色为透明🎯 设置窗口位置🎯 创建用于显示时间的标签 📝 更新时间函数📝 使窗口可移动📝 设置窗口置顶📝 完整代码 ⚓️ 相关链接 ⚓️ 📖 介绍 📖 在项目开发中,有时我们需要创建无边

电子杂志制作的必备软件:轻松提升制作效率

​电子杂志作为一种新型的媒体形式,具有互动性强、内容丰富、传播范围广等特点。随着互联网的普及,越来越多的企业和个人开始关注和投入电子杂志的制作。然而,电子杂志的制作过程往往复杂繁琐,需要付出大量的时间和精力。为了提高电子杂志的制作效率,我们可以借助一些专业的软件工具。 1.要制作电子杂志,首先需要选择一款适合自己的软件。比如FLBOOK在线制作电子杂志平台。这个工具具有强大的功能