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

Commit

Permalink
Update 支持从网站上图片拖拽创建截图
Browse files Browse the repository at this point in the history
  • Loading branch information
tylearymf committed Jan 17, 2021
1 parent ce1d83e commit 0a251b5
Show file tree
Hide file tree
Showing 9 changed files with 548 additions and 400 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ A best screenshot small tool (support high dpi screenshots)

- ###### 配置方法:选项 -> 杂项设置 -> 其他 -> 显示放大镜

- 支持从网站上图片拖拽创建截图

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

---

## 目前已知问题
Expand Down
95 changes: 81 additions & 14 deletions SETUNA/Main/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,49 @@ public static Bitmap FromPath(string path)
{
if (File.Exists(path))
{

byte[] buffer = null;
MemoryStream stream = null;
Bitmap bitmap = null;

try
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var bitmap = new Bitmap(fs);
bitmap.MakeTransparent();
return bitmap;
buffer = new byte[fs.Length];
stream = new MemoryStream(buffer);
fs.CopyTo(stream);
}
}
catch (ArgumentException ex)
{
try

var imageType = ImageUtils.GetImageType(buffer);
switch (imageType)
{
using (var webp = new WebPWrapper.WebP())
{
var bitmap = webp.Load(path);
bitmap.MakeTransparent();
case ImageType.PNG:
bitmap = new Bitmap(stream);
bitmap.MakeTransparent(Color.White);
return bitmap;
case ImageType.WEBP:
using (var webp = new WebPWrapper.WebP())
{
bitmap = webp.Decode(buffer);
return bitmap;
}
default:
bitmap = new Bitmap(stream);
return bitmap;
}
}
catch { }
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

finally
{
if (stream != null)
{
stream.Dispose();
}
}
}

return null;
Expand Down Expand Up @@ -114,5 +130,56 @@ static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509C
return true;
}
}

static class ImageUtils
{
public static ImageType GetImageType(byte[] imageBuffer)
{
if (imageBuffer.Length > 3 &&
imageBuffer[1] == 0x50 &&
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
)
{
return ImageType.JPEG;
}
else if (imageBuffer.Length > 10 &&
imageBuffer[8] == 0x57 &&
imageBuffer[9] == 0x45 &&
imageBuffer[10] == 0x42
)
{
return ImageType.WEBP;
}
else if (imageBuffer.Length > 2 &&
imageBuffer[0] == 0x47 &&
imageBuffer[1] == 0x49 &&
imageBuffer[2] == 0x46
)
{
return ImageType.GIF;
}

return ImageType.Unknown;
}
}

public enum ImageType
{
Unknown,
JPEG,
PNG,
WEBP,
GIF,
}
}

791 changes: 411 additions & 380 deletions SETUNA/Main/Option/OptionForm.Designer.cs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions SETUNA/Main/Option/OptionForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private void LoadSetunaOption()
radioButton1_fullscreenCursor.Checked = _so.Setuna.FullscreenCursorSolid;
radioButton2_fullscreenCursor.Checked = !_so.Setuna.FullscreenCursorSolid;
pictureBox_fullscreenCursor.BackColor = _so.Setuna.FullscreenCursorLineColor;

checkBox_transparent.Checked = _so.Setuna.BackgroundTransparentEnabled;
}

// Token: 0x060002D5 RID: 725 RVA: 0x00013908 File Offset: 0x00011B08
Expand Down Expand Up @@ -153,6 +155,13 @@ private void WriteSetunaOption()
_so.Setuna.FullscreenCursorColorR = pictureBox_fullscreenCursor.BackColor.R;
_so.Setuna.FullscreenCursorColorG = pictureBox_fullscreenCursor.BackColor.G;
_so.Setuna.FullscreenCursorColorB = pictureBox_fullscreenCursor.BackColor.B;

_so.Setuna.BackgroundTransparentEnabled = checkBox_transparent.Checked;

foreach (var item in Mainform.Instance.scrapBook)
{
item.Refresh();
}
}

// Token: 0x060002D6 RID: 726 RVA: 0x00013D84 File Offset: 0x00011F84
Expand Down
3 changes: 0 additions & 3 deletions SETUNA/Main/Option/OptionForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,4 @@
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
5 changes: 5 additions & 0 deletions SETUNA/Main/Option/SetunaOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static SetunaOption GetDefaultOption()
setunaOption.Setuna.FullscreenCursorColorR = Color.Orange.R;
setunaOption.Setuna.FullscreenCursorColorG = Color.Orange.G;
setunaOption.Setuna.FullscreenCursorColorB = Color.Orange.B;
setunaOption.Setuna.BackgroundTransparentEnabled = false;


var cstyle = new CStyle
Expand Down Expand Up @@ -1036,6 +1037,8 @@ public SetunaOptionData()
FullscreenCursorColorR = Color.Orange.R;
FullscreenCursorColorG = Color.Orange.G;
FullscreenCursorColorB = Color.Orange.B;

BackgroundTransparentEnabled = false;
}

// Token: 0x17000072 RID: 114
Expand Down Expand Up @@ -1120,6 +1123,8 @@ public SetunaOptionData()

public bool MagnifierEnabled;

public bool BackgroundTransparentEnabled;

public Color FullscreenCursorLineColor => Color.FromArgb(FullscreenCursorColorR, FullscreenCursorColorG, FullscreenCursorColorB);
public bool FullscreenCursorSolid;
public byte FullscreenCursorColorR;
Expand Down
16 changes: 15 additions & 1 deletion SETUNA/Main/ScrapBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,21 @@ protected override void OnPaint(PaintEventArgs e)
var all = Padding.All;
e.Graphics.InterpolationMode = _interpolationmode;
//e.Graphics.DrawImage(imgView, all, all, width, height);
e.Graphics.DrawImage(imgView, all, all, Width - all * 2, Height - all * 2);

if (Mainform.Instance.optSetuna.Setuna.BackgroundTransparentEnabled)
{
e.Graphics.Clear(Color.Green);
TransparencyKey = Color.Green;

e.Graphics.DrawImage(imgView, all, all, Width - all * 2, Height - all * 2);
}
else
{
e.Graphics.Clear(Color.White);

e.Graphics.DrawImage(imgView, all, all, Width, Height);
}

if (!_solidframe)
{
var pen = new Pen(Color.FromArgb(243, 243, 243));
Expand Down
22 changes: 20 additions & 2 deletions SETUNA/Main/StyleItems/CImageStyleItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace SETUNA.Main.StyleItems
// Token: 0x0200000E RID: 14
public abstract class CImageStyleItem : CStyleItem
{
static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare, PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed, PixelFormat.Format8bppIndexed };

// Token: 0x060000C6 RID: 198 RVA: 0x00005711 File Offset: 0x00003911
public CImageStyleItem()
{
Expand All @@ -22,16 +24,27 @@ public CImageStyleItem()
public override void Apply(ref ScrapBase scrap, Point clickpoint)
{
Bitmap bitmap = null;
Graphics graphics = null;
var text = "";
try
{
if (HaveMargin)
{
bitmap = ((Bitmap)scrap.GetViewImage()).Clone(new Rectangle(0, 0, scrap.Width, scrap.Height), PixelFormat.Format24bppRgb);
bitmap = new Bitmap(scrap.Width, scrap.Height, PixelFormat.Format24bppRgb);
graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(scrap.GetViewImage(), 0, 0, scrap.Width, scrap.Height);
}
else
{
bitmap = ((Bitmap)scrap.Image).Clone(new Rectangle(0, 0, scrap.Image.Width, scrap.Image.Height), scrap.Image.PixelFormat);
var pixelFormat = scrap.Image.PixelFormat;
if (Array.IndexOf(indexedPixelFormats, pixelFormat) != -1)
{
pixelFormat = PixelFormat.Format24bppRgb;
}

bitmap = new Bitmap(scrap.Image.Width, scrap.Image.Height, pixelFormat);
graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(scrap.Image, 0, 0, scrap.Image.Width, scrap.Image.Height);
}
var encoderParams = GetEncoderParams(ref scrap);
if (encoderParams != null)
Expand Down Expand Up @@ -76,6 +89,11 @@ public override void Apply(ref ScrapBase scrap, Point clickpoint)
}
finally
{
if (graphics != null)
{
graphics.Dispose();
}

if (bitmap != null)
{
bitmap.Dispose();
Expand Down
3 changes: 3 additions & 0 deletions SETUNA/Main/StyleItems/CompactScrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private void CompactScrap_DoubleClick(object sender, EventArgs e)
// Token: 0x0600040B RID: 1035 RVA: 0x0001A014 File Offset: 0x00018214
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.Green);
TransparencyKey = Color.Green;

e.Graphics.DrawImageUnscaled(_thumbnail, new Point(-_clickpoint.X + base.Width / 2, -_clickpoint.Y + base.Height / 2));
e.Graphics.DrawRectangle(Pens.White, new Rectangle(0, 0, base.Width - 1, base.Height - 1));
e.Graphics.DrawRectangle(_pen, new Rectangle(0, 0, base.Width - 1, base.Height - 1));
Expand Down

0 comments on commit 0a251b5

Please sign in to comment.