Подтвердить что ты не робот

С# - Преобразование WPF Image.source в System.Drawing.Bitmap

Я нашел множество людей, преобразующих BitmapSource в Bitmap, но как насчет ImageSource - Bitmap? Я делаю программу обработки изображений, и мне нужно извлечь растровые изображения из изображения, отображаемого в элементе Image. Кто-нибудь знает, как это сделать?

ИЗМЕНИТЬ 1:

Это функция преобразования BitmapImage в Bitmap. Не забудьте установить параметр "небезопасно" в настройках компилятора.

public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource srs)
{
    System.Drawing.Bitmap btm = null;

    int width = srs.PixelWidth;

    int height = srs.PixelHeight;

    int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);

    byte[] bits = new byte[height * stride];

    srs.CopyPixels(bits, stride, 0);

    unsafe
    {
        fixed (byte* pB = bits)
        {
            IntPtr ptr = new IntPtr(pB);

            btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr);
        }
    }
    return btm;
}

Теперь нужно получить BitmapImage:

RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
    (int)inkCanvas1.ActualWidth,
    (int)inkCanvas1.ActualHeight,
    96d, 96d,
    PixelFormats.Default);

targetBitmap.Render(inkCanvas1);

MemoryStream mse = new MemoryStream();
System.Windows.Media.Imaging.BmpBitmapEncoder mem = new BmpBitmapEncoder();
mem.Frames.Add(BitmapFrame.Create(targetBitmap));
mem.Save(mse);

mse.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = mse;
bi.EndInit();

Далее следует преобразовать его:

Bitmap b = new Bitmap(BitmapSourceToBitmap(bi));
4b9b3361

Ответ 1

На самом деле вам не нужно использовать небезопасный код. Там перегрузка CopyPixels, которая принимает IntPtr:

public static System.Drawing.Bitmap BitmapSourceToBitmap2(BitmapSource srs)
{
    int width = srs.PixelWidth;
    int height = srs.PixelHeight;
    int stride = width * ((srs.Format.BitsPerPixel + 7) / 8);
    IntPtr ptr = IntPtr.Zero;
    try
    {
        ptr = Marshal.AllocHGlobal(height * stride);
        srs.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride);
        using (var btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, ptr))
        {
            // Clone the bitmap so that we can dispose it and
            // release the unmanaged memory at ptr
            return new System.Drawing.Bitmap(btm);
        }
    }
    finally
    {
        if (ptr != IntPtr.Zero)
            Marshal.FreeHGlobal(ptr);
    }
}

Ответ 2

Является ли ваш ImageSource не BitmapSource? Если вы загружаете изображения из файлов, они должны быть.

Ответить на комментарий:

Похоже, что они должны быть BitmapSource, тогда BitmapSource является подтипом ImageSource. Отправляйте ImageSource в BitmapSource и следуйте одному из этих blogposts.

Ответ 3

Вам вообще не нужен метод BitmapSourceToBitmap. После создания потока памяти выполните следующие действия:

mem.Position = 0;  
Bitmap b = new Bitmap(mem);

Ответ 4

Этот пример работал у меня:

    public static Bitmap ConvertToBitmap(BitmapSource bitmapSource)
    {
        var width = bitmapSource.PixelWidth;
        var height = bitmapSource.PixelHeight;
        var stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
        var memoryBlockPointer = Marshal.AllocHGlobal(height * stride);
        bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), memoryBlockPointer, height * stride, stride);
        var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, memoryBlockPointer);
        return bitmap;
    }