Skip to content

Commit

Permalink
#89 #77 Fix Issue
Browse files Browse the repository at this point in the history
Co-Authored-By: Deakin Tsang <35422654+deakintsang@users.noreply.github.com>

#89 #77 Fix Issue
  • Loading branch information
yanjinhuagood committed Feb 5, 2024
1 parent a349551 commit 4139ff4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/WPFDevelopers.Shared/Controls/ScreenCut/ScreenCut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
using Brushes = System.Windows.Media.Brushes;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Drawing.Imaging;
using System.Windows.Threading;

namespace WPFDevelopers.Controls
{
Expand Down Expand Up @@ -248,7 +250,7 @@ public override void OnApplyTemplate()
_canvas.Width = Screen.AllScreens[ScreenIndex].Bounds.Width;
_canvas.Height = Screen.AllScreens[ScreenIndex].Bounds.Height;
//_canvas.Background = new ImageBrush(ControlsHelper.Capture());
_canvas.Background = new ImageBrush(ConvertBitmap(CopyScreen()));
_canvas.Background = new ImageBrush(ImagingHelper.CreateBitmapSourceFromBitmap(CopyScreen()));
_rectangleLeft.Width = _canvas.Width;
_rectangleLeft.Height = _canvas.Height;
_border.Opacity = 0;
Expand Down Expand Up @@ -289,19 +291,7 @@ private void ScreenCut_Loaded(object sender, RoutedEventArgs e)
{

}
private BitmapSource ConvertBitmap(Bitmap bitmap)
{
BitmapSource img;
IntPtr hBitmap;
hBitmap = bitmap.GetHbitmap();
img = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
img.Freeze();
return img;
}

private ScreenDPI GetScreenDPI(int screenIndex)
{
ScreenDPI dpi = new ScreenDPI();
Expand Down
58 changes: 58 additions & 0 deletions src/WPFDevelopers.Shared/Core/Helpers/ImagingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace WPFDevelopers
{
public static class ImagingHelper
{
public static BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap)
{
if (bitmap == null)
throw new ArgumentNullException("bitmap");

if (Application.Current.Dispatcher == null)
return null;
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
memoryStream.Seek(0, SeekOrigin.Begin);
if (InvokeRequired)
return (BitmapSource)Application.Current.Dispatcher.Invoke(
new Func<Stream, BitmapSource>(CreateBitmapSourceFromBitmap),
DispatcherPriority.Normal,
memoryStream);
return CreateBitmapSourceFromBitmap(memoryStream);
}
}
catch (Exception)
{
return null;
}
}

private static bool InvokeRequired
{
get { return Dispatcher.CurrentDispatcher != Application.Current.Dispatcher; }
}

private static BitmapSource CreateBitmapSourceFromBitmap(Stream stream)
{
var bitmapDecoder = BitmapDecoder.Create(
stream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);

var writable = new WriteableBitmap(bitmapDecoder.Frames.Single());
writable.Freeze();
return writable;
}
}
}
Binary file modified src/WPFDevelopers.Shared/GZ/WPFDevelopersExt.exe.gz
Binary file not shown.

0 comments on commit 4139ff4

Please sign in to comment.