Click here to Skip to main content
15,891,431 members
Home / Discussions / C#
   

C#

 
AnswerRe: Image Annotation in C# Pin
Pete O'Hanlon16-May-10 11:01
mvePete O'Hanlon16-May-10 11:01 
GeneralRe: Image Annotation in C# Pin
snouto16-May-10 11:52
snouto16-May-10 11:52 
GeneralRe: Image Annotation in C# Pin
Pete O'Hanlon16-May-10 21:37
mvePete O'Hanlon16-May-10 21:37 
AnswerRe: Image Annotation in C# Pin
Luc Pattyn16-May-10 11:43
sitebuilderLuc Pattyn16-May-10 11:43 
AnswerRe: Image Annotation in C# Pin
Ravi Bhavnani16-May-10 13:30
professionalRavi Bhavnani16-May-10 13:30 
QuestionPorting a Windows Forms project to Mono Pin
User 691845416-May-10 10:24
User 691845416-May-10 10:24 
AnswerRe: Porting a Windows Forms project to Mono Pin
OriginalGriff16-May-10 21:45
mveOriginalGriff16-May-10 21:45 
QuestionHaving problems with C# and OpenGL interoperability Pin
Keith Vitali16-May-10 9:04
Keith Vitali16-May-10 9:04 
Hello everyone,

I am trying to use some managed code with some unmanaged rendering functions using OpenGL. I have a C# bitmap and I am trying to use OpenGL functions to draw on this bitmap.

Currently, my problem is that I am unable to initialize OpenGL and it fails with the error "Attempted to read or write protected memory. This is often an indication that the other memory is corrupt".

My OpenGL initialization routine is as follows (this is implemented as a C++ DLL function):

extern "C" __declspec(dllexport) BOOL InitGL(HDC * hdc,
                                              HGLRC * hglrc)
{   
    BOOL retval = FALSE;    
    PIXELFORMATDESCRIPTOR pfd;
    int format;    
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;	    
    format = ChoosePixelFormat(*hdc, &pfd );
    retval = SetPixelFormat(*hdc, format, &pfd);    
    *hglrc = wglCreateContext(*hdc);    
    return retval;
}


The important bit here is the use of PFD_DRAW_TO_BITMAP and PFD_SUPPORT_GDI. I have tried drawing directly to window (using PFD_DRAW_TO_WINDOW) and that works ok.

It is called from C# as follows (I am omitting the DLLImport sections here and just focussing on the call).

Additionally, _bitmap is the bitmap object that I want to draw on.

try
{
    Graphics graphics = System.Drawing.Graphics.FromImage(_bitmap);
    IntPtr hDC = graphics.GetHdc();
    // If we have not initialized OpenGL, do it now.
    if (!native_init)
    {
        NativeMethods.InitGL(hDC, out this._hglrc);
        native_init = true;
    }
}
finally 
{
}


I thought that maybe I need to lock the bitmap data and I tried the following:

try
{
    BitmapData bitmapData = _bitmap.LockBits(
                new Rectangle(0, 0, _bitmap.Width, _bitmap.Height),
                ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    Graphics graphics = System.Drawing.Graphics.FromImage(_bitmap);
    IntPtr hDC = graphics.GetHdc();
    // If we have not initialized OpenGL, do it now.
    if (!native_init)
    {
        NativeMethods.InitGL(hDC, out this._hglrc);
        native_init = true;
    }
}
finally 
{
    _bitmap.UnlockBits(bitmapData);
}


However, this fails with the same error. I am not sure how I can use this locked bitmap data to initialize the OpenGL context.

Any help here would be really appreciated. I have spend hours looking at this and trolling the net to no avail now!

Thanks,

Keith
AnswerRe: Having problems with C# and OpenGL interoperability Pin
Keith Vitali16-May-10 11:44
Keith Vitali16-May-10 11:44 
QuestionImplicit Variables Pin
Darrall16-May-10 6:51
Darrall16-May-10 6:51 
AnswerRe: Implicit Variables Pin
Abhinav S16-May-10 7:22
Abhinav S16-May-10 7:22 
GeneralRe: Implicit Variables Pin
Darrall16-May-10 7:33
Darrall16-May-10 7:33 
AnswerRe: Implicit Variables Pin
riced16-May-10 8:12
riced16-May-10 8:12 
GeneralRe: Implicit Variables Pin
Darrall16-May-10 10:28
Darrall16-May-10 10:28 
QuestionSystem.Globalization and Provinces Pin
Ryan Minor16-May-10 6:11
Ryan Minor16-May-10 6:11 
AnswerRe: System.Globalization and Provinces Pin
dan!sh 16-May-10 6:19
professional dan!sh 16-May-10 6:19 
AnswerRe: System.Globalization and Provinces Pin
Abhinav S16-May-10 6:35
Abhinav S16-May-10 6:35 
AnswerRe: System.Globalization and Provinces Pin
Pete O'Hanlon16-May-10 10:21
mvePete O'Hanlon16-May-10 10:21 
QuestionSystem.Globalization and ComboBox of Countries Pin
Ryan Minor16-May-10 5:36
Ryan Minor16-May-10 5:36 
AnswerRe: System.Globalization and ComboBox of Countries Pin
OriginalGriff16-May-10 5:44
mveOriginalGriff16-May-10 5:44 
GeneralRe: System.Globalization and ComboBox of Countries [modified] Pin
Luc Pattyn16-May-10 6:05
sitebuilderLuc Pattyn16-May-10 6:05 
GeneralRe: System.Globalization and ComboBox of Countries Pin
OriginalGriff16-May-10 21:47
mveOriginalGriff16-May-10 21:47 
Questionauto generate code number Pin
FEMDEV16-May-10 1:59
FEMDEV16-May-10 1:59 
AnswerUrgent Pin
Not Active16-May-10 2:33
mentorNot Active16-May-10 2:33 
GeneralRe: Urgent Pin
FEMDEV16-May-10 2:56
FEMDEV16-May-10 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.