Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image to Image from .net bitmap #5

Open
nigelianburton opened this issue Apr 27, 2024 · 1 comment
Open

Image to Image from .net bitmap #5

nigelianburton opened this issue Apr 27, 2024 · 1 comment

Comments

@nigelianburton
Copy link

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?

@DarthAffe
Copy link
Owner

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 ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants