Reputation: 33
I tried to capture my screen in C# (.NET 4.6.2 in console mode) to get a base64 string of the bitmap.
My issue is that the base64 is too big in my case so I want to decrease the resolution of the Bitmap, I tried this code:
public static string TakeScreenshotToBase64()
{
Bitmap memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Size size = new Size(memoryImage.Width, memoryImage.Height);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, size);
MemoryStream ms = new MemoryStream();
memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return Convert.ToBase64String(ms.ToArray());
}
If I try to edit the width and the height values, I got a smaller picture but I don't have the full picture sadly.
So I tried to create a new Bitmap from my existing Bitmap with another resolution, but it doesn't work, any suggestions? thank you for your help.
Upvotes: 2
Views: 565
Reputation: 162
As requested, i hope this helps. I can explain better if the comments are not enough.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace BitMapResize
{
public partial class Form1 : Form
{
/// <summary>
/// Function to Create the images.
/// </summary>
/// <param name="resizeWidth">If it is null, the application will create an image with the original size.</param>
/// <param name="resizeHeight">If it is null, the application will create an image with the original size.</param>
public static void TakeScreenshotToBase64(float? resizeWidth, float? resizeHeight)
{
using (MemoryStream ms = new MemoryStream())
{
/* First things first, creating a bitmap of the original data to the memorystream, so we can use later.*/
Bitmap memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Size size = new Size(memoryImage.Width, memoryImage.Height);
using (Graphics memoryGraphics = Graphics.FromImage(memoryImage))
{
memoryGraphics.CopyFromScreen(0, 0, 0, 0, size); // Persisting the full screen resolution.
memoryImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // Saving in the memory stream...
}
string imageName = "Print"; // This won't change if the size is the original.
// This conditions checks if the resize parameters are not null. If they are not null, a resizing process begins.
if (!(resizeWidth is null) || !(resizeHeight is null))
{
var bmp = new Bitmap((int)resizeWidth.Value, (int)resizeHeight.Value); // Create a new bitmap with the new dimensions.
using (Graphics memoryGraphics = Graphics.FromImage(bmp)) // Using a Graphics that operates on the new bitmap.
{
// Defining scale factor. This way we maintain aspect ratio.
float scale = Math.Min(resizeWidth.Value / memoryImage.Width, resizeHeight.Value / memoryImage.Height);
int scaleWidth = (int)(memoryImage.Width * scale);
int scaleHeight = (int)(memoryImage.Height * scale);
// Optional: Set this for better output quality.
memoryGraphics.InterpolationMode = InterpolationMode.High;
memoryGraphics.CompositingQuality = CompositingQuality.HighQuality;
memoryGraphics.SmoothingMode = SmoothingMode.AntiAlias;
// Here, choose the background color for the image.
/* Why the background?
* As long as we keep the aspect ratio, if the given dimension does not respect the scale of the original dimension,
* an area of the new image will not be filled. So we can set a background color for these empty areas.
*/
Brush baseColor = new SolidBrush(Color.White);
memoryGraphics.FillRectangle(baseColor, new RectangleF(0, 0, resizeWidth.Value, resizeHeight.Value));
// Process the resize, based on the in-memory buffer that we wrote earlier.
memoryGraphics.DrawImage(Image.FromStream(ms), ((int)resizeWidth.Value - scaleWidth) / 2, ((int)resizeHeight.Value - scaleHeight) / 2, scaleWidth, scaleHeight);
}
imageName = "ResizedPrint";
// Not requested, but now can save this in a file.
using (FileStream fs = new FileStream(Application.StartupPath + "/" + imageName + ".jpg", FileMode.Create))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else
{
using (FileStream fs = new FileStream(Application.StartupPath + "/" + imageName + ".jpg", FileMode.Create))
{
// If no resize was done, save the original data.
memoryImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
// Print event start.
private void button1_Click(object sender, EventArgs e)
{
TakeScreenshotToBase64(1024, 768);
//TakeScreenshotToBase64(null, null);
}
}
}
Upvotes: 1