Open nigelianburton opened 4 months ago
Since System.Drawing is windows only, .NET bitmaps will not be supported directly in the "core"-library. I'm planning on adding more helper for image handling and conversion as additional packages though, but this will take some more time.
Until then you can use this extension:
public static unsafe Image<ColorRGB> ToImage(this Bitmap bitmap)
{
int width = bitmap.Width;
int height = bitmap.Height;
byte[] buffer = new byte[height * width * ColorRGB.ColorFormat.BytesPerPixel];
Span<ColorRGB> colorBuffer = MemoryMarshal.Cast<byte, ColorRGB>(buffer);
Rectangle rect = new(0, 0, bitmap.Width, bitmap.Height);
BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
nint ptr = bmpData.Scan0;
for (int y = 0; y < height; y++)
{
Span<ColorBGR> source = new((void*)ptr, bmpData.Stride);
Span<ColorRGB> target = colorBuffer.Slice(y * width, width);
for (int x = 0; x < width; x++)
{
ColorBGR srcColor = source[x];
target[x] = new ColorRGB(srcColor.R, srcColor.G, srcColor.B);
}
ptr += bmpData.Stride;
}
bitmap.UnlockBits(bmpData);
return new Image<ColorRGB>(buffer, 0, 0, width, height, width);
}
and then call it like this:
using Bitmap bitmap = new(@"<path to image>");
Image<ColorRGB> sourceImage = bitmap.ToImage();
using StableDiffusionImage image = model.ImageToImage("<prompt>", sourceImage.ToArray(), new StableDiffusionParameter());
Overall this is not an ideal solution in terms of performance and usability but I should work for now ~
Thanks for bringing Stable Diffusion to .NET. The library includes helpers to convert TextToImage output into a .NET bitmap image, but I can't see any way to run ImageToImage from a .NET bitmap. Did I miss it/is it something you plan?