Solved

.NET - SetPixel

Posted on 2002-03-31
16
730 Views
Last Modified: 2013-11-25
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.PaintEventArgs) 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(myBitmap, 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(myBitmap, 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.
0
Comment
Question by:Azmodan
16 Comments
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
Have you the proper reference at the top of your code module to import the System.Drawing namespace?
0
 

Author Comment

by:Azmodan
Comment Utility
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?

0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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?
0
 

Author Comment

by:Azmodan
Comment Utility
release

i tried this in C# and in VB... same result
that's why I think it's a bug in .NET framework
0
 

Author Comment

by:Azmodan
Comment Utility
?
AzraSound... no news on this subject? It's kind of important to me. Have u tried the SetPixel method in a VB, C# sample?
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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.
0
 

Author Comment

by:Azmodan
Comment Utility
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?
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Azmodan
Comment Utility
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.
0
 

Author Comment

by:Azmodan
Comment Utility
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$.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
If I find out anything, I will let you know.
0
 

Author Comment

by:Azmodan
Comment Utility
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...
0
 

Author Comment

by:Azmodan
Comment Utility
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).
0
 

Author Comment

by:Azmodan
Comment Utility
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().
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
0
 
LVL 1

Accepted Solution

by:
kodiakbear earned 0 total points
Comment Utility
Moving to the PAQ

kb
Experts Exchange Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now