Apply region path to image with anti aliasing

This commit is contained in:
Jaex 2016-08-02 11:29:17 +03:00
parent a1a1c1ef17
commit 24ada5cc9a
5 changed files with 16 additions and 61 deletions

View file

@ -243,30 +243,6 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
return null; return null;
} }
public static Image CropImage(Image img, Rectangle rect, GraphicsPath gp)
{
if (img != null && rect.Width > 0 && rect.Height > 0 && gp != null)
{
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.SetHighQuality();
using (Region region = new Region(gp))
{
g.Clip = region;
g.DrawImage(img, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
}
}
return bmp;
}
return null;
}
public static Image DrawOutline(Image img, GraphicsPath gp) public static Image DrawOutline(Image img, GraphicsPath gp)
{ {
if (img != null && gp != null) if (img != null && gp != null)

View file

@ -232,7 +232,7 @@ public virtual Image GetResultImage()
{ {
using (Image img = GetOutputImage()) using (Image img = GetOutputImage())
{ {
return RegionCaptureHelpers.ApplyRegionPathToImage(img, regionFillPath, Config); return RegionCaptureHelpers.ApplyRegionPathToImage(img, regionFillPath);
} }
} }
else if (Result == RegionResult.Fullscreen) else if (Result == RegionResult.Fullscreen)

View file

@ -24,7 +24,6 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using ShareX.HelpersLib; using ShareX.HelpersLib;
using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Windows.Forms; using System.Windows.Forms;
@ -145,29 +144,28 @@ public static void ShowScreenRuler()
} }
} }
public static Image ApplyRegionPathToImage(Image backgroundImage, GraphicsPath regionFillPath, RegionCaptureOptions options) public static Image ApplyRegionPathToImage(Image img, GraphicsPath gp)
{ {
if (backgroundImage != null && regionFillPath != null) if (img != null && gp != null)
{ {
Image img; Rectangle regionArea = Rectangle.Round(gp.GetBounds());
Rectangle regionArea = Rectangle.Round(regionFillPath.GetBounds());
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based(); Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
Rectangle newRegionArea = Rectangle.Intersect(regionArea, screenRectangle); regionArea = Rectangle.Intersect(regionArea, screenRectangle);
using (GraphicsPath gp = (GraphicsPath)regionFillPath.Clone()) if (regionArea.IsValid())
{ {
using (Matrix matrix = new Matrix()) using (Bitmap bmp = img.CreateEmptyBitmap())
using (Graphics g = Graphics.FromImage(bmp))
using (TextureBrush brush = new TextureBrush(img))
{ {
gp.CloseFigure(); g.PixelOffsetMode = PixelOffsetMode.Half;
matrix.Translate(-Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y)); g.SmoothingMode = SmoothingMode.HighQuality;
gp.Transform(matrix);
g.FillPath(brush, gp);
return ImageHelpers.CropBitmap(bmp, regionArea);
} }
img = ImageHelpers.CropImage(backgroundImage, newRegionArea, gp);
} }
return img;
} }
return null; return null;

View file

@ -1479,25 +1479,6 @@ public bool IsShapeIntersect()
return GetShapeIntersect() != null; return GetShapeIntersect() != null;
} }
public Rectangle CombineAreas()
{
BaseShape[] areas = ValidRegions;
if (areas.Length > 0)
{
Rectangle rect = areas[0].Rectangle;
for (int i = 1; i < areas.Length; i++)
{
rect = Rectangle.Union(rect, areas[i].Rectangle);
}
return rect;
}
return Rectangle.Empty;
}
private void UpdateCursor() private void UpdateCursor()
{ {
try try

View file

@ -2282,7 +2282,7 @@ private void CaptureLastRegion(TaskSettings taskSettings, bool autoHideForm = tr
{ {
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen()) using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{ {
return RegionCaptureHelpers.ApplyRegionPathToImage(screenshot, BaseRegionForm.LastRegionFillPath, taskSettings.CaptureSettings.SurfaceOptions); return RegionCaptureHelpers.ApplyRegionPathToImage(screenshot, BaseRegionForm.LastRegionFillPath);
} }
}, CaptureType.LastRegion, taskSettings, autoHideForm); }, CaptureType.LastRegion, taskSettings, autoHideForm);
} }