Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
liesauer committed Dec 2, 2020
0 parents commit 14f9347
Show file tree
Hide file tree
Showing 22 changed files with 3,034 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# macOS
.DS_Store
__MACOSX

# vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# vs
.vs/

# Build results
[Bb]in/
[Oo]bj/
[Rr]elease/

# sublime
*.sublime-workspace
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CPOL-1.02
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h1 align="center">微信表情包工具</h1>
<h3 align="center">快速将QQ或其他表情包转换成微信可添加的表情</h3>

## 举个栗子

### 普通图片

![screenshot_01.png](screenshot_01.png)

### 转换为表情包

![screenshot_01.png](screenshot_02.png)

## 支持图片格式

`png` `jpg` `bmp`

不支持大图以及透明图,微信原生支持`gif`动图直接添加为表情,所以不需要转换。

## 代码引用

[NGif, Animated GIF Encoder for .NET](https://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET)
31 changes: 31 additions & 0 deletions pic2meme.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.421
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pic2meme", "pic2meme\pic2meme.csproj", "{0DAF5660-6734-4678-B69D-AF988F733AFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Debug|x86.ActiveCfg = Debug|x86
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Debug|x86.Build.0 = Debug|x86
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Release|Any CPU.Build.0 = Release|Any CPU
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Release|x86.ActiveCfg = Release|x86
{0DAF5660-6734-4678-B69D-AF988F733AFB}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1E55E386-C1C8-404A-A66E-D3D39DC7342B}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions pic2meme/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="pic2meme.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:pic2meme"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
16 changes: 16 additions & 0 deletions pic2meme/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace pic2meme
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
}
}
13 changes: 13 additions & 0 deletions pic2meme/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window x:Class="pic2meme.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:pic2meme"
mc:Ignorable="d"
Title="微信表情包工具" Height="364.4" Width="550" AllowDrop="True" Drop="Window_Drop" KeyDown="Window_KeyDown">
<Grid>
<Label x:Name="Notice" Content="拖动或粘贴图片到此处进行表情包转换" FontSize="22" Margin="20,20,20,0" VerticalAlignment="Top" Height="54" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Image x:Name="Preview" HorizontalAlignment="Left" Height="200" Margin="175,106,0,0" VerticalAlignment="Top" Width="200" Stretch="Uniform" />
</Grid>
</Window>
179 changes: 179 additions & 0 deletions pic2meme/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Drawing;
using System.Drawing.Imaging;
using Gif.Components;
using Image = System.Drawing.Image;
using System.IO;
using System.Collections.Specialized;

namespace pic2meme
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private enum ConvertMode
{
QuickConvert,
CompatibleConvert,
}

private ConvertMode convertMode = ConvertMode.CompatibleConvert;

public MainWindow()
{
InitializeComponent();
this.MinWidth = this.MaxWidth = this.Width;
this.MinHeight = this.MaxHeight = this.Height;
}

private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.V)
{
HandleDataObject(Clipboard.GetDataObject());
}
}

private void Window_Drop(object sender, DragEventArgs e)
{
HandleDataObject(e.Data);
}

private void HandleDataObject(IDataObject dataObject)
{
try
{
if (dataObject.GetDataPresent(DataFormats.FileDrop))
{
var file = ((System.Array)dataObject.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
pic2meme(file);
}
else if (dataObject.GetDataPresent(DataFormats.Bitmap))
{
var image = dataObject.GetData(DataFormats.Bitmap) as BitmapSource;
var tmp = SaveBitmapSource(image);
pic2meme(tmp);
}
}
catch
{
}
}

private string pic2meme(string image)
{
Notice.Content = "转换中...";

var tmpImage = GenerateTempFile(".gif");

if (convertMode == ConvertMode.QuickConvert)
{
try
{
File.Copy(image, tmpImage);
}
catch
{
tmpImage = null;
}
}
else
{
try
{
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.Start(tmpImage);
//e.SetQuality(10);
e.SetDelay(1000);
e.SetRepeat(-1);
if (File.Exists(image))
{
e.AddFrame(Image.FromFile(image));
}
else
{
tmpImage = null;
}
e.Finish();
}
catch
{
tmpImage = null;
}
}

if (!string.IsNullOrEmpty(tmpImage))
{
Notice.Content = "转换成功,直接粘贴到微信发送即可";
SetImageToClipboard(tmpImage);
SetPreview(tmpImage);
}
else
{
Notice.Content = "转换失败";
}

return tmpImage;
}

private string SaveBitmapSource(BitmapSource source)
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));

var tmp = GenerateTempFile(".jpg");

try
{
using (var stream = new FileStream(tmp, FileMode.Create))
{
encoder.Save(stream);
}
}
catch
{
tmp = null;
}

return tmp;
}

private string GenerateTempFile(string ext)
{
var file = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString().ToUpper() + ext;
return File.Exists(file) ? GenerateTempFile(ext) : file;
}

private void SetPreview(string imageFile)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(imageFile);
bitmapImage.EndInit();
Preview.Source = bitmapImage;
}

private void SetImageToClipboard(string imageFile)
{
var list = new StringCollection
{
imageFile
};
Clipboard.SetFileDropList(list);
}
}
}
Loading

0 comments on commit 14f9347

Please sign in to comment.