Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Update 支持图片格式 PSD、ICO、WEBP、SVG、TGA、SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
tylearymf committed Jan 17, 2021
1 parent 0a251b5 commit bac075e
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 125 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ A best screenshot small tool (support high dpi screenshots)
- 支持从网站上图片拖拽创建截图

- ###### 从网站拖拽图片到某个截图中会自动创建截图(该功能需要联网,因为需要下载对应的网站图片)

- 支持从图片格式 **JPEG****PNG****PSD****GIF****BMP****ICO****TIFF****WEBP****SVG****TGA****SVG** 中创建截图

---

Expand Down
30 changes: 22 additions & 8 deletions SETUNA/Main/CaptureForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,26 @@ public CaptureForm.CaptureClosedDelegate OnCaptureClose
set => CaptureClosedHandler = value;
}

public static Image ImgSnap
{
get => imgSnap;
set
{
if (imgSnap != null)
{
imgSnap.Dispose();
}

imgSnap = value;
}
}

// Token: 0x0600029C RID: 668 RVA: 0x0000DEA8 File Offset: 0x0000C0A8
public CaptureForm(SetunaOption.SetunaOptionData opt)
{
InitializeComponent();
targetScreen = GetCurrentScreen();
CaptureForm.imgSnap = new Bitmap(targetScreen.Bounds.Width, targetScreen.Bounds.Height, PixelFormat.Format24bppRgb);
CaptureForm.ImgSnap = new Bitmap(targetScreen.Bounds.Width, targetScreen.Bounds.Height, PixelFormat.Format24bppRgb);
CaptureForm.selArea = new Form
{
AutoScaleMode = AutoScaleMode.None,
Expand Down Expand Up @@ -178,15 +192,15 @@ void InitChildForm(Form form)
// Token: 0x0600029D RID: 669 RVA: 0x0000E104 File Offset: 0x0000C304
public void ShowCapture(SetunaOption.SetunaOptionData opt)
{
if (CaptureForm.imgSnap == null)
if (CaptureForm.ImgSnap == null)
{
return;
}
//Cursor.Current = Cursors.Cross;
targetScreen = GetCurrentScreen();
if (targetScreen.Bounds.Width != CaptureForm.imgSnap.Width || targetScreen.Bounds.Height != CaptureForm.imgSnap.Height)
if (targetScreen.Bounds.Width != CaptureForm.ImgSnap.Width || targetScreen.Bounds.Height != CaptureForm.ImgSnap.Height)
{
CaptureForm.imgSnap = new Bitmap(targetScreen.Bounds.Width, targetScreen.Bounds.Height, PixelFormat.Format24bppRgb);
CaptureForm.ImgSnap = new Bitmap(targetScreen.Bounds.Width, targetScreen.Bounds.Height, PixelFormat.Format24bppRgb);
}
trd = new Thread(new ThreadStart(ThreadTask))
{
Expand Down Expand Up @@ -287,7 +301,7 @@ private Screen GetCurrentScreen()
// Token: 0x0600029F RID: 671 RVA: 0x0000E680 File Offset: 0x0000C880
private void ThreadTask()
{
var flag = CaptureForm.CopyFromScreen(CaptureForm.imgSnap, new Point(targetScreen.Bounds.X, targetScreen.Bounds.Y));
var flag = CaptureForm.CopyFromScreen(CaptureForm.ImgSnap, new Point(targetScreen.Bounds.X, targetScreen.Bounds.Y));
if (flag)
{
base.Invoke(new CaptureForm.ShowFormDelegate(ShowForm));
Expand Down Expand Up @@ -491,9 +505,9 @@ private void CaptureForm_VisibleChanged(object sender, System.EventArgs e)
// Token: 0x060002A8 RID: 680 RVA: 0x0000EBDF File Offset: 0x0000CDDF
private void CaptureForm_Paint(object sender, PaintEventArgs e)
{
if (CaptureForm.imgSnap != null)
if (CaptureForm.ImgSnap != null)
{
e.Graphics.DrawImageUnscaled(CaptureForm.imgSnap, 0, 0);
e.Graphics.DrawImageUnscaled(CaptureForm.ImgSnap, 0, 0);
}
}

Expand Down Expand Up @@ -700,7 +714,7 @@ private void CreateClip(Point pt, Size size)
bmpClip = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
using (var graphics = Graphics.FromImage(bmpClip))
{
graphics.DrawImageUnscaled(CaptureForm.imgSnap, -(pt.X - targetScreen.Bounds.X), -(pt.Y - targetScreen.Bounds.Y));
graphics.DrawImageUnscaled(CaptureForm.ImgSnap, -(pt.X - targetScreen.Bounds.X), -(pt.Y - targetScreen.Bounds.Y));
}
}

Expand Down
141 changes: 124 additions & 17 deletions SETUNA/Main/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Media.Imaging;
using Svg;

namespace SETUNA.Main
{
Expand Down Expand Up @@ -51,25 +53,48 @@ public static Bitmap FromPath(string path)
buffer = new byte[fs.Length];
stream = new MemoryStream(buffer);
fs.CopyTo(stream);
stream.Seek(0, SeekOrigin.Begin);
}

var imageType = ImageUtils.GetImageType(buffer);
switch (imageType)
{
case ImageType.PNG:
bitmap = new Bitmap(stream);
bitmap.MakeTransparent(Color.White);
return bitmap;
break;
case ImageType.WEBP:
using (var webp = new WebPWrapper.WebP())
{
bitmap = webp.Decode(buffer);
return bitmap;
}
break;
case ImageType.SVG:
bitmap = SvgDocument.Open<SvgDocument>(stream).Draw();
break;
case ImageType.PSD:
var psdFile = new System.Drawing.PSD.PsdFile();
psdFile.Load(path);
bitmap = System.Drawing.PSD.ImageDecoder.DecodeImage(psdFile);
break;
case ImageType.ICO:
using (var icon = new Icon(path))
{
bitmap = icon.ToBitmap();
}
break;
case ImageType.TGA:
using (var reader = new BinaryReader(stream))
{
var image = new TgaLib.TgaImage(reader);
bitmap = image.GetBitmap().ToBitmap();
}
break;
default:
bitmap = new Bitmap(stream);
return bitmap;
break;
}

return bitmap;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -116,6 +141,19 @@ public static void DownloadImage(string url, Action<Bitmap> finished)
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36";
client.DownloadFileAsync(new Uri(url), filePath);
}

public static Bitmap ToBitmap(this BitmapSource source)
{
Bitmap bitmap;
using (var outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(source));
enc.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}
}

static class NetUtils
Expand All @@ -137,49 +175,118 @@ public static ImageType GetImageType(byte[] imageBuffer)
{
if (imageBuffer.Length > 3 &&
imageBuffer[1] == 0x50 &&
imageBuffer[2] == 0x4E &&
imageBuffer[3] == 0x47
)
imageBuffer[2] == 0x4E &&
imageBuffer[3] == 0x47)
{
return ImageType.PNG;
}
else if (imageBuffer.Length > 9 &&
imageBuffer[6] == 0x4A &&
imageBuffer[7] == 0x46 &&
imageBuffer[8] == 0x49 &&
imageBuffer[9] == 0x46
)
imageBuffer[7] == 0x46 &&
imageBuffer[8] == 0x49 &&
imageBuffer[9] == 0x46)
{
return ImageType.JPEG;
}
else if (imageBuffer.Length > 10 &&
imageBuffer[8] == 0x57 &&
imageBuffer[9] == 0x45 &&
imageBuffer[10] == 0x42
)
imageBuffer[9] == 0x45 &&
imageBuffer[10] == 0x42)
{
return ImageType.WEBP;
}
else if (imageBuffer.Length > 2 &&
imageBuffer[0] == 0x47 &&
imageBuffer[1] == 0x49 &&
imageBuffer[2] == 0x46
)
imageBuffer[1] == 0x49 &&
imageBuffer[2] == 0x46)
{
return ImageType.GIF;
}
else if (imageBuffer.Length > 2 &&
imageBuffer[1] == 0x73 &&
imageBuffer[2] == 0x76 &&
imageBuffer[3] == 0x67)
{
return ImageType.SVG;
}
else if (imageBuffer.Length > 2 &&
imageBuffer[0] == 0x38 &&
imageBuffer[1] == 0x42 &&
imageBuffer[2] == 0x50 &&
imageBuffer[3] == 0x53)
{
return ImageType.PSD;
}
else if (imageBuffer.Length > 5 &&
imageBuffer[0] == 0x00 &&
imageBuffer[1] == 0x00 &&
imageBuffer[2] == 0x01 &&
imageBuffer[3] == 0x00 &&
imageBuffer[4] == 0x01 &&
imageBuffer[5] == 0x00)
{
return ImageType.ICO;
}
else if (TGAUtils.IsTGA(imageBuffer))
{
return ImageType.TGA;
}

return ImageType.Unknown;
}
}

static class TGAUtils
{
public static bool IsTGA(byte[] imageBuffer)
{
if (imageBuffer.Length > 16)
{
var imageType = imageBuffer[2];
var colorMapDepth = imageBuffer[7];
var pixelDepth = imageBuffer[16];

switch (imageType)
{
case 1:
case 9:
if (colorMapDepth - 15 <= 1 || colorMapDepth == 24 || colorMapDepth == 32)
{
return true;
}
break;
case 2:
case 10:
if (pixelDepth - 15 <= 1 || pixelDepth == 24 || pixelDepth == 32)
{
return true;
}
break;
case 3:
case 11:
if (pixelDepth == 8)
{
return true;
}
break;
}
}

return false;
}
}

public enum ImageType
{
Unknown,
JPEG,
PNG,
WEBP,
GIF,
SVG,
PSD,
ICO,
TGA,
}
}

2 changes: 1 addition & 1 deletion SETUNA/Main/ScrapSourceUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override Image GetImage()

public override Point GetPosition()
{
return point;
return point;
}

// Token: 0x0400010F RID: 271
Expand Down
1 change: 0 additions & 1 deletion SETUNA/Plugins/ResourceExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;

namespace SETUNA.Plugins
{
Expand Down
Loading

0 comments on commit bac075e

Please sign in to comment.