Skip to content

Commit c1ef38f

Browse files
committed
Bug fixes.
1 parent f4f1c8e commit c1ef38f

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,19 @@ public bool PreFilterMessage(ref Message m)
473473
_windowHandle = windowHandle;
474474
scale = _scale;
475475
}
476-
using (var image = WindowUtils.CaptureWindow(windowHandle))
476+
if (scale == 1m)
477477
{
478-
var newImage = ImageUtils.Reduce(image, (int)(image.Width * scale), (int)(image.Height * scale));
478+
var newImage = WindowUtils.CaptureWindow(windowHandle);
479479
FillImage(newImage);
480480
}
481+
else
482+
{
483+
using (var image = WindowUtils.CaptureWindow(windowHandle))
484+
{
485+
var newImage = ImageUtils.ResizeImage(image, (int)(image.Width * scale), (int)(image.Height * scale));
486+
FillImage(newImage);
487+
}
488+
}
481489
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
482490
FillInformation(windowInformation);
483491
OnContentChanged();
@@ -532,9 +540,16 @@ private void CaptureWindowCallback()
532540
{
533541
if (imageTab || isRecording)
534542
{
535-
using (var sourceImage = WindowUtils.CaptureWindow(windowHandle))
543+
if (scale == 1m)
536544
{
537-
newImage = ImageUtils.Reduce(sourceImage, (int)(sourceImage.Width * scale), (int)(sourceImage.Height * scale));
545+
newImage = WindowUtils.CaptureWindow(windowHandle);
546+
}
547+
else
548+
{
549+
using (var sourceImage = WindowUtils.CaptureWindow(windowHandle))
550+
{
551+
newImage = ImageUtils.ResizeImage(sourceImage, (int)(sourceImage.Width * scale), (int)(sourceImage.Height * scale));
552+
}
538553
}
539554
}
540555
}
@@ -600,13 +615,25 @@ private void UpdateButtonCallback()
600615

601616
private void WriteVideoFrameCallback()
602617
{
603-
lock (_lockObject)
618+
try
604619
{
605-
if (_isRecording)
620+
lock (_lockObject)
606621
{
607-
_videoWriter.WriteVideoFrame(_image);
622+
if (_isRecording)
623+
{
624+
_videoWriter.WriteVideoFrame(_image);
625+
}
608626
}
609627
}
628+
catch (ArgumentException e) when (e.Message.Contains("size must be of the same as video size"))
629+
{
630+
BeginInvoke((MethodInvoker)delegate
631+
{
632+
MessageBox.Show("Don't resize the window while recording.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
633+
});
634+
635+
throw;
636+
}
610637
}
611638

612639
private void InitTimers(int fps)
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
using System.Drawing;
22
using System.Drawing.Drawing2D;
3+
using System.Drawing.Imaging;
34

45
namespace WindowTextExtractor.Utils
56
{
67
static class ImageUtils
78
{
8-
public static Bitmap Reduce(Bitmap source, int width, int height)
9+
/// <summary>
10+
/// Resize the image to the specified width and height.
11+
/// </summary>
12+
/// <param name="image">The image to resize.</param>
13+
/// <param name="width">The width to resize to.</param>
14+
/// <param name="height">The height to resize to.</param>
15+
/// <returns>The resized image.</returns>
16+
public static Bitmap ResizeImage(Image image, int width, int height)
917
{
10-
var bitmap = new Bitmap(width, height);
11-
using (var graphics = Graphics.FromImage(bitmap))
18+
var destRect = new Rectangle(0, 0, width, height);
19+
var destImage = new Bitmap(width, height);
20+
21+
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
22+
23+
using (var graphics = Graphics.FromImage(destImage))
1224
{
25+
graphics.CompositingMode = CompositingMode.SourceCopy;
26+
graphics.CompositingQuality = CompositingQuality.HighQuality;
1327
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
14-
graphics.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(0, 0, source.Width, source.Height), GraphicsUnit.Pixel);
28+
graphics.SmoothingMode = SmoothingMode.HighQuality;
29+
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
30+
31+
using (var wrapMode = new ImageAttributes())
32+
{
33+
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
34+
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
35+
}
1536
}
16-
return bitmap;
37+
38+
return destImage;
1739
}
1840
}
1941
}

0 commit comments

Comments
 (0)