Skip to content

Commit

Permalink
add image quality slider to control image compression ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
EslaMx7 committed Feb 22, 2022
1 parent fd053d1 commit f19914a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
1 change: 1 addition & 0 deletions ScreenTask/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class AppSettings
public bool IsShowMouseEnabled { get; set; }
public bool IsAutoStartServerEnabled { get; set; }
public bool IsStartMinimizedEnabled { get; set; }
public int ImageQuality { get; set; }
}
}
70 changes: 51 additions & 19 deletions ScreenTask/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 32 additions & 5 deletions ScreenTask/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private async Task StartServer()
var fileinfo = new FileInfo(page);
if (fileinfo.Extension == ".css") // important for IE -> Content-Type must be defiend for CSS files unless will ignored !!!
ctx.Response.ContentType = "text/css";
else if (fileinfo.Extension == ".svg")
else if (fileinfo.Extension == ".svg")
ctx.Response.ContentType = "image/svg+xml";
else if (fileinfo.Extension == ".html" || fileinfo.Extension == ".htm")
ctx.Response.ContentType = "text/html"; // Important For Chrome Otherwise will display the HTML as plain text.
Expand Down Expand Up @@ -254,32 +254,52 @@ private async Task CaptureScreenEvery(int msec)
}
private void TakeScreenshot(bool captureMouse)
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

var encoderQuality = System.Drawing.Imaging.Encoder.Quality;
var encoderParam = new EncoderParameter(encoderQuality, _currentSettings.ImageQuality);
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = encoderParam;
if (captureMouse)
{
var bmp = ScreenCapturePInvoke.CaptureSelectedScreen(true, comboScreens.SelectedIndex);
rwl.AcquireWriterLock(Timeout.Infinite);
bmp.Save(Application.StartupPath + "/WebServer" + "/ScreenTask.jpg", ImageFormat.Jpeg);
bmp.Save(Application.StartupPath + "/WebServer" + "/ScreenTask.jpg", jpgEncoder, encoderParams);
rwl.ReleaseWriterLock();

bmp.Dispose();
bmp = null;
return;
}

Rectangle bounds = Screen.AllScreens[comboScreens.SelectedIndex].Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.X,bounds.Y), Point.Empty, bounds.Size);
g.CopyFromScreen(new Point(bounds.X, bounds.Y), Point.Empty, bounds.Size);
}
rwl.AcquireWriterLock(Timeout.Infinite);
bitmap.Save(Application.StartupPath + "/WebServer" + "/ScreenTask.jpg", ImageFormat.Jpeg);

bitmap.Save(Application.StartupPath + "/WebServer" + "/ScreenTask.jpg", jpgEncoder, encoderParams);
rwl.ReleaseWriterLock();


}
}

private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
private string GetIPv4Address()
{
string IP4Address = String.Empty;
Expand Down Expand Up @@ -427,6 +447,7 @@ private void frmMain_Load(object sender, EventArgs e)
this.txtPassword.Text = _currentSettings.Password;
this.numPort.Value = _currentSettings.Port;
this.numShotEvery.Value = _currentSettings.ScreenshotsSpeed;
this.qualitySlider.Value = _currentSettings.ImageQuality != default ? _currentSettings.ImageQuality : 75;
this.comboIPs.SelectedIndex = _ips.IndexOf(_ips.FirstOrDefault(ip => ip.Item2.Contains(_currentSettings.IP)));
if (_currentSettings.SelectedScreenIndex > -1 && comboScreens.Items.Count > 0 && _currentSettings.SelectedScreenIndex <= comboScreens.Items.Count - 1)
this.comboScreens.SelectedIndex = _currentSettings.SelectedScreenIndex;
Expand Down Expand Up @@ -497,6 +518,7 @@ private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
_currentSettings.ScreenshotsSpeed = (int)numShotEvery.Value;
_currentSettings.IP = _ips.ElementAt(comboIPs.SelectedIndex).Item2;
_currentSettings.SelectedScreenIndex = comboScreens.SelectedIndex;
_currentSettings.ImageQuality = qualitySlider.Value;

using (var appSettingsFile = new FileStream("appsettings.xml", FileMode.Create, FileAccess.Write))
{
Expand Down Expand Up @@ -541,5 +563,10 @@ private void appNotify_Click(object sender, EventArgs e)
this.Focus();
}

private void qualitySlider_Scroll(object sender, EventArgs e)
{
_currentSettings.ImageQuality = qualitySlider.Value;

}
}
}

0 comments on commit f19914a

Please sign in to comment.