Solved

.NET - SetPixel

Posted on 2002-03-31
16
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 28

Expert Comment

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

Author Comment

by:Azmodan
ID: 6910182
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
ID: 6910201
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Azmodan
ID: 6910215
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
ID: 6968370
?
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
ID: 6968423
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
ID: 6968455
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
ID: 6968465
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
 

Author Comment

by:Azmodan
ID: 6968495
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
ID: 6968584
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
ID: 6968596
If I find out anything, I will let you know.
0
 

Author Comment

by:Azmodan
ID: 6976696
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
ID: 7005529
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
ID: 7050900
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
ID: 7642860
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
ID: 7673385
Moving to the PAQ

kb
Experts Exchange Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PL SQL Developer 7 75
Need a simple RegEx to search for two words 27 86
SSRS Deployment problem 5 97
how to loop through and process two columns in excel 8 53
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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