implement conversion method for images and imagedata to System.Drawing.Bitmap
Shadowblitz16 opened this issue · comments
Shadowblitz16 commented
please implement ToBitmap() for love images and image data.
it would return a system.drawing.bitmap
Yx commented
Here the System.Drawing.Bitmap
does't support in .net standard 1.2
:
The System.Drawing.Bitmap
have compatibility issues on cross platform, so i will not add them to Love2dCS
library. Excuse me for the inconvenience .
But i will give you code to implement it and here is the code to do that :
using System.Windows.Forms;
using Love;
namespace Project1
{
class Issue91 : Scene
{
/// <summary>
/// convert Image to Bitmap
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static System.Drawing.Bitmap ToBitmap(Image image)
{
var canvas = Graphics.NewCanvas(image.GetWidth(), image.GetHeight());
var oldTargets = Graphics.GetCanvas();
Graphics.SetCanvas(canvas);
Graphics.Draw(image);
Graphics.SetCanvas(oldTargets);
var imageData = canvas.NewImageData();
return ToBitmap(imageData);
}
/// <summary>
/// convert ImageData to Bitmap
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static System.Drawing.Bitmap ToBitmap(ImageData imageData)
{
var dataBytes = imageData.Encode(ImageFormat.PNG).GetBytes();
var stream = new System.IO.MemoryStream(dataBytes);
var bitmap = new System.Drawing.Bitmap(stream);
return bitmap;
}
Form form = null;
public override void Load()
{
// create image
var imageData = Image.NewImageData(100, 100);
imageData.MapPixel((x, y, c) =>
{
return new Color(x / 100f, y / 100f, 1, 1);
});
var bitmap1 = ToBitmap(imageData);
imageData.MapPixel((x, y, c) =>
{
return new Color(x / 100f, 1, y / 100f, 1);
});
var bitmap2 = ToBitmap(Graphics.NewImage(imageData));
// draw to form
form = new System.Windows.Forms.Form();
form.Show();
var g = form.CreateGraphics();
g.DrawImage(bitmap1, new System.Drawing.PointF());
g.DrawImage(bitmap2, new System.Drawing.PointF(100, 100));
}
}
}
Shadowblitz16 commented
ah ok thankyou for the info
Yx commented
close by it finished.