fixed a bug of Screenshot on win7

This commit is contained in:
NaBian committed 2020-04-18 23:22:39 +08:00
1 parent dc64d5cacb
commit f4087574c0
11 files changed
+850 -450

No files matched your search

@@ -0,0 +1,18 @@
using System;
namespace HandyControl.Data
{
[Flags]
internal enum ImageCodecFlags
{
Encoder = 0x00000001,
Decoder = 0x00000002,
SupportBitmap = 0x00000004,
SupportVector = 0x00000008,
SeekableEncode = 0x00000010,
BlockingDecode = 0x00000020,
Builtin = 0x00010000,
System = 0x00020000,
User = 0x00040000
}
}
@@ -3,12 +3,12 @@ using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using HandyControl.Tools;
using HandyControl.Tools.Interop;
namespace HandyControl.Data
{
// ReSharper disable once InconsistentNaming
internal class GPStream : ExternDllHelper.IStream
internal class GPStream : InteropValues.IStream
{
protected Stream DataStream;
@@ -55,7 +55,7 @@ namespace HandyControl.Data
_virtualPosition = -1;
}
public virtual ExternDllHelper.IStream Clone()
public virtual InteropValues.IStream Clone()
{
NotImplemented();
return null;
@@ -72,7 +72,7 @@ namespace HandyControl.Data
UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows),
SecurityPermission(SecurityAction.Assert, Flags = SecurityPermissionFlag.UnmanagedCode)
]
public virtual long CopyTo(ExternDllHelper.IStream pstm, long cb, long[] pcbRead)
public virtual long CopyTo(InteropValues.IStream pstm, long cb, long[] pcbRead)
{
const int bufsize = 4096;
@@ -117,7 +117,7 @@ namespace HandyControl.Data
protected static ExternalException EFail(string msg)
{
throw new ExternalException(msg, ExternDllHelper.E_FAIL);
throw new ExternalException(msg, InteropMethods.E_FAIL);
}
protected static void NotImplemented()
@@ -154,7 +154,7 @@ namespace HandyControl.Data
var len = DataStream.Length;
switch (origin)
{
case ExternDllHelper.StreamConsts.STREAM_SEEK_SET:
case InteropValues.StreamConsts.STREAM_SEEK_SET:
if (offset <= len)
{
DataStream.Position = offset;
@@ -165,7 +165,7 @@ namespace HandyControl.Data
_virtualPosition = offset;
}
break;
case ExternDllHelper.StreamConsts.STREAM_SEEK_END:
case InteropValues.StreamConsts.STREAM_SEEK_END:
if (offset <= 0)
{
DataStream.Position = len + offset;
@@ -176,7 +176,7 @@ namespace HandyControl.Data
_virtualPosition = len + offset;
}
break;
case ExternDllHelper.StreamConsts.STREAM_SEEK_CUR:
case InteropValues.StreamConsts.STREAM_SEEK_CUR:
if (offset + pos <= len)
{
DataStream.Position = pos + offset;
@@ -1,6 +1,6 @@
using System;
using System.Runtime.InteropServices;
using HandyControl.Tools;
using HandyControl.Tools.Interop;
namespace HandyControl.Data
{
@@ -85,7 +85,7 @@ namespace HandyControl.Data
GifPropertyItemInternal propcopy = null;
try
{
propcopy = (GifPropertyItemInternal) ExternDllHelper.PtrToStructure(propdata,
propcopy = (GifPropertyItemInternal) InteropMethods.PtrToStructure(propdata,
typeof(GifPropertyItemInternal));
props[i] = new GifPropertyItem
@@ -0,0 +1,158 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using HandyControl.Tools.Interop;
namespace HandyControl.Data
{
internal sealed class ImageCodecInfo
{
private string _dllName;
public Guid Clsid { get; set; }
public Guid FormatID { get; set; }
public string CodecName { get; set; }
public string DllName
{
[SuppressMessage("Microsoft.Security", "CA2103:ReviewImperativeSecurity")]
get
{
if (_dllName != null)
{
new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.PathDiscovery, _dllName).Demand();
}
return _dllName;
}
[SuppressMessage("Microsoft.Security", "CA2103:ReviewImperativeSecurity")]
set
{
if (value != null)
{
new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.PathDiscovery, value).Demand();
}
_dllName = value;
}
}
public string FormatDescription { get; set; }
public string FilenameExtension { get; set; }
public string MimeType { get; set; }
public ImageCodecFlags Flags { get; set; }
public int Version { get; set; }
public byte[][] SignaturePatterns { get; set; }
public byte[][] SignatureMasks { get; set; }
public static ImageCodecInfo[] GetImageDecoders()
{
ImageCodecInfo[] imageCodecs;
var status = InteropMethods.Gdip.GdipGetImageDecodersSize(out var numDecoders, out var size);
if (status != InteropMethods.Gdip.Ok)
{
throw InteropMethods.Gdip.StatusException(status);
}
var memory = Marshal.AllocHGlobal(size);
try
{
status = InteropMethods.Gdip.GdipGetImageDecoders(numDecoders, size, memory);
if (status != InteropMethods.Gdip.Ok)
{
throw InteropMethods.Gdip.StatusException(status);
}
imageCodecs = ConvertFromMemory(memory, numDecoders);
}
finally
{
Marshal.FreeHGlobal(memory);
}
return imageCodecs;
}
public static ImageCodecInfo[] GetImageEncoders()
{
ImageCodecInfo[] imageCodecs;
var status = InteropMethods.Gdip.GdipGetImageEncodersSize(out var numEncoders, out var size);
if (status != InteropMethods.Gdip.Ok)
{
throw InteropMethods.Gdip.StatusException(status);
}
var memory = Marshal.AllocHGlobal(size);
try
{
status = InteropMethods.Gdip.GdipGetImageEncoders(numEncoders, size, memory);
if (status != InteropMethods.Gdip.Ok)
{
throw InteropMethods.Gdip.StatusException(status);
}
imageCodecs = ConvertFromMemory(memory, numEncoders);
}
finally
{
Marshal.FreeHGlobal(memory);
}
return imageCodecs;
}
public static ImageCodecInfo[] ConvertFromMemory(IntPtr memoryStart, int numCodecs)
{
var codecs = new ImageCodecInfo[numCodecs];
int index;
for (index = 0; index < numCodecs; index++)
{
var curcodec = (IntPtr)((long)memoryStart + Marshal.SizeOf(typeof(InteropValues.ImageCodecInfoPrivate)) * index);
var codecp = new InteropValues.ImageCodecInfoPrivate();
InteropMethods.PtrToStructure(curcodec, codecp);
codecs[index] = new ImageCodecInfo
{
Clsid = codecp.Clsid,
FormatID = codecp.FormatID,
CodecName = Marshal.PtrToStringUni(codecp.CodecName),
DllName = Marshal.PtrToStringUni(codecp.DllName),
FormatDescription = Marshal.PtrToStringUni(codecp.FormatDescription),
FilenameExtension = Marshal.PtrToStringUni(codecp.FilenameExtension),
MimeType = Marshal.PtrToStringUni(codecp.MimeType),
Flags = (ImageCodecFlags) codecp.Flags,
Version = codecp.Version,
SignaturePatterns = new byte[codecp.SigCount][],
SignatureMasks = new byte[codecp.SigCount][]
};
for (var j = 0; j < codecp.SigCount; j++)
{
codecs[index].SignaturePatterns[j] = new byte[codecp.SigSize];
codecs[index].SignatureMasks[j] = new byte[codecp.SigSize];
Marshal.Copy((IntPtr)((long)codecp.SigMask + j * codecp.SigSize), codecs[index].SignatureMasks[j], 0, codecp.SigSize);
Marshal.Copy((IntPtr)((long)codecp.SigPattern + j * codecp.SigSize), codecs[index].SignaturePatterns[j], 0, codecp.SigSize);
}
}
return codecs;
}
}
}