I have an overloaded method which gets the bytes in an uploaded image that seems to be causing my entire application to resize. This only seems to happen when the display scaling is anything other than 100%.
The weird thing is that the application resizes when it enters the overloaded method but before any actual code in the method is run. Even stranger, if I set the DPI scale to 100%, do the usual actions that would resize the form, and then switch the display scaling back to 125% then the resizing does not happen.
This is the method:
public static byte[] GetMultiPageImageBytes(byte[] imageBytes, double? rotationAngle, string subject)
{
using (MemoryStream inStream = new MemoryStream(imageBytes))
{
return GetMultiPageImageBytes(inStream, rotationAngle, subject);
}
}
and the overloaded method(where the resizing occurs):
public static byte[] GetMultiPageImageBytes(Stream inStream, double? rotationAngle, string subject)
{ /************************ APP RESIZES ON THIS LINE ************************/
BitmapDecoder decoder = new TiffBitmapDecoder(inStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
foreach (BitmapFrame frame in decoder.Frames)
{
BitmapMetadata metadata = null;
if (subject != null)
{
metadata = new BitmapMetadata("tiff");
metadata.Subject = subject;
}
TransformedBitmap rotatedImage = null;
if (rotationAngle != null)
{
System.Windows.Media.RotateTransform transform = new System.Windows.Media.RotateTransform(rotationAngle.Value);
rotatedImage = new TransformedBitmap(frame, transform);
}
BitmapSource sourceImage = rotatedImage != null ? (BitmapSource)rotatedImage : (BitmapSource)frame;
BitmapFrame newFrame = BitmapFrame.Create(sourceImage, frame.Thumbnail, metadata, frame.ColorContexts);
encoder.Frames.Add(newFrame);
}
using (MemoryStream outStream = new MemoryStream())
{
encoder.Save(outStream);
return outStream.ToArray();
}
}
I've tried SetProcessDPIAware()
which got rid of the problem but that messed up the layout of dozens of other forms and components. The application is too big in order to optimize for DPI awareness at this point as I would need to reformat dozens of components.
I also tried removing the using
statement in the original method which calls the overloaded method and just manually disposing the stream but that didn't work either:
// Didn't work
public static byte[] GetMultiPageImageBytes(byte[] imageBytes, double? rotationAngle, string subject)
{
MemoryStream inStream = new MemoryStream(imageBytes);
byte[] bytes;
bytes = GetMultiPageImageBytes(inStream, rotationAngle, subject);
inStream.Dispose();
inStream.Close();
return bytes;
}
Any ideas as to what might be causing this? Any help with this would be greatly appreciated!
Disabling the DPI awareness based on @Jimi suggestion worked for me. I ended up using code from this answer: WPF ClickOnce DPI awareness Per-Monitor v2 from @Marko