Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > Ошыбка "A generic error occurred in GDI+" при прео


Автор: virtualRem777 11.2.2008, 16:58
Ошыбка "A generic error occurred in GDI+" при преобразовании картинки в byte[]

Автор: Alekz 12.2.2008, 10:13
А подробнее?
Хотябы опиши, что ты пытаешься сделать.

Автор: virtualRem777 12.2.2008, 11:35
формат изображения jpg

private static byte[] ConvertImageToByteArray(Image im)
        {
            byte[] Ret;

            try
            {

                using (MemoryStream ms = new MemoryStream())
                {
                    imageToConvert.Save(ms, formatOfImage);
                    Ret = ms.ToArray();//строка в которой появляється ошыбка
                }
            }
            catch (Exception) { throw; }

            return Ret;            
        }

Автор: Alekz 12.2.2008, 12:04
А откуда ты берешь объект Image?

Глянь сюда, может помочь:
http://support.microsoft.com/?id=814675

Автор: virtualRem777 13.2.2008, 18:26
Обьект Image беру из ячейки datagridview

Автор: Alekz 14.2.2008, 11:28
Я понимаю (http://forum.vingrad.ru/forum/topic-194873.html)   smile

Но я имел в виду, каким образом создаются объекты Image. Если из файла (или другого потока), то очень вероятно что дело в этом:

Цитата

if the stream was destroyed during the life of the Bitmap object, you cannot successfully access an image that was based on a stream
...
GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object.


т.е. поток, из которого создан Image , должен существовать.

там же, чуть дальше рекомендуемое решение:
Цитата

1.    Construct the original Bitmap from the stream, from the memory, or from the file.
2.    Create a new Bitmap of the same size, with a pixel format of more than 8 bits-per-pixel (BPP).
3.    Use the Graphics.FromImage() method to obtain a Graphics object for the second Bitmap.
4.    Use Graphics.DrawImage() to draw the first Bitmap onto the second Bitmap.
5.    Use Graphics.Dispose() to dispose of the Graphics.
6.    Use Bitmap.Dispose() to dispose of the first Bitmap.



Код

Bitmap b1 = Bitmap.FromStream(stream);
Bitmap b2 = new Bitmap(b1.Width, b1.Height);
Graphics g = Graphics.FromImage(b2);
g.DrawImage(b1);
g.Dispose();
b1.Dispose();
stream.Close();


теперь можно работать с  b2.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)