Azmodan
asked on
.NET - SetPixel
Hy,
I found that the old PSet method in VB6 has been replaced with the Bitmap.SetPixel class.
So I tryed the example in the documentation, but it doesn't work. It compiles ok, but at run-time it gives an error - Invalid Argument used (exception in system.windows.forms.dll for VB, system.drawing.dll in C#)
Has anyone been able to use SetPixel? How?
Here is the sample from the documentation
I just put a picturebox on a form and edit the Paint event.
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.Paint EventArgs) Handles PictureBox1.Paint
' Create a Bitmap object from a file.
Dim myBitmap As New Bitmap("d:\ok.gif")
' Draw myBitmap to the screen.
e.Graphics.DrawImage(myBit map, 0, 0, myBitmap.Width, _
myBitmap.Height)
' Set each pixel in myBitmap to black.
Dim Xcount As Integer
For Xcount = 0 To myBitmap.Width - 1
Dim Ycount As Integer
For Ycount = 0 To myBitmap.Height - 1
myBitmap.SetPixel(Xcount, Ycount, Color.Black)
Next Ycount
Next Xcount
' Draw myBitmap to the screen again.
e.Graphics.DrawImage(myBit map, myBitmap.Width, 0, myBitmap.Width, _
myBitmap.Height)
End Sub
Remarque: 1.if I comment out the SetPixel line, it works.
2. Even if I hardcode it SetPixel(1,1,Color.Black) it doesn't work.
I found that the old PSet method in VB6 has been replaced with the Bitmap.SetPixel class.
So I tryed the example in the documentation, but it doesn't work. It compiles ok, but at run-time it gives an error - Invalid Argument used (exception in system.windows.forms.dll for VB, system.drawing.dll in C#)
Has anyone been able to use SetPixel? How?
Here is the sample from the documentation
I just put a picturebox on a form and edit the Paint event.
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.Paint
' Create a Bitmap object from a file.
Dim myBitmap As New Bitmap("d:\ok.gif")
' Draw myBitmap to the screen.
e.Graphics.DrawImage(myBit
myBitmap.Height)
' Set each pixel in myBitmap to black.
Dim Xcount As Integer
For Xcount = 0 To myBitmap.Width - 1
Dim Ycount As Integer
For Ycount = 0 To myBitmap.Height - 1
myBitmap.SetPixel(Xcount, Ycount, Color.Black)
Next Ycount
Next Xcount
' Draw myBitmap to the screen again.
e.Graphics.DrawImage(myBit
myBitmap.Height)
End Sub
Remarque: 1.if I comment out the SetPixel line, it works.
2. Even if I hardcode it SetPixel(1,1,Color.Black) it doesn't work.
Have you the proper reference at the top of your code module to import the System.Drawing namespace?
ASKER
yes
the code compiles
An Exception appears only at run-time
the interesting detail is that, if I press Break (in the IDE) the debug cursor is green (not yellow) and is positioned on the next statement after SetPixel
is this a bug in VS.NET?
the code compiles
An Exception appears only at run-time
the interesting detail is that, if I press Break (in the IDE) the debug cursor is green (not yellow) and is positioned on the next statement after SetPixel
is this a bug in VS.NET?
I can look into this when I get home...I do not have .NET loaded at work. What build of .NET are you running? Beta 1, 2, release?
ASKER
release
i tried this in C# and in VB... same result
that's why I think it's a bug in .NET framework
i tried this in C# and in VB... same result
that's why I think it's a bug in .NET framework
ASKER
?
AzraSound... no news on this subject? It's kind of important to me. Have u tried the SetPixel method in a VB, C# sample?
AzraSound... no news on this subject? It's kind of important to me. Have u tried the SetPixel method in a VB, C# sample?
My apologies...I had lost my reference to this question. I have only the Beta1 version of .NET, but you are right, that sample does not work. I played around with it a bit, and could not get it to work either. Part of the problem may be that I am still sitting on the Beta1 version. However, it seems there is a good chance this is one of the many bugs I'm sure are to come with their new release, or, we are just missing a concept about the correct use of SetPixel.
ASKER
Well... if the sample taked from the documentation with copy-paste doesn't work, i would say it's a bug.
Does M$ know about this?
Does M$ know about this?
I have no idea...it wouldn't hurt to write them about it (though, from my experience, they are not always very prompt to get back to you)
ASKER
ok...first of all, I just noticed there is allready a Service Pack 1 for .NET framework. This may corect the problem.
Second... the SetPixel is just a wraper for the GDI SetPixel method. There is also a SetPixel method in the MFC class HBitMap. So... I think that it could be done by using the GDI function after getting somehow the HDC handler.
Second... the SetPixel is just a wraper for the GDI SetPixel method. There is also a SetPixel method in the MFC class HBitMap. So... I think that it could be done by using the GDI function after getting somehow the HDC handler.
ASKER
i installed .NET SP1. It didn't solved the problem. :(
I wouldn't know how\where to write to MS. If you have more experience in this field, please take this problem with M$.
I wouldn't know how\where to write to MS. If you have more experience in this field, please take this problem with M$.
If I find out anything, I will let you know.
ASKER
I succeded in calling the API SetPixel and it works.
Still the framework SetPixel doesn't work.
And I didn't find any way to tell MS about this...
Still the framework SetPixel doesn't work.
And I didn't find any way to tell MS about this...
ASKER
new stuff...
SetPixel DOES work IF the picture loaded in the picturebox is a JPG or PNG, but not if it's a BMP, GIF or TIF. So this clearly is a BUG
other:
If the .net bitmap.setPixel is used, it modifies the memory representation of the image, so a refresh is needed to see the efect.
If the GDI SetPixel is used, it affects only the screen representation and the PictureBox object is not changed (that is.. if i hide the window and than show it again, the modifications disaper).
SetPixel DOES work IF the picture loaded in the picturebox is a JPG or PNG, but not if it's a BMP, GIF or TIF. So this clearly is a BUG
other:
If the .net bitmap.setPixel is used, it modifies the memory representation of the image, so a refresh is needed to see the efect.
If the GDI SetPixel is used, it affects only the screen representation and the PictureBox object is not changed (that is.. if i hide the window and than show it again, the modifications disaper).
ASKER
new stuff (2)..
it seems that SetPixel is not restricted to some image formats, but to wherever the Image has transparency or not. By calling Bitmap.MakeTransparent(), SetPixel works on all images. The transparency can be checked with IsAlphaPixelFormat().
it seems that SetPixel is not restricted to some image formats, but to wherever the Image has transparency or not. By calling Bitmap.MakeTransparent(), SetPixel works on all images. The transparency can be checked with IsAlphaPixelFormat().
Azmodan, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:
Refund points and save as a 0-pt PAQ.
DanRollins -- EE database cleanup volunteer
Moderator, my recommended disposition is:
Refund points and save as a 0-pt PAQ.
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.