.Re: SetPixel. See this Microsoft example

Hi,

I am trying to change a pixel color inside a gif file image, using the setpixel and this failed with the error -

SetPixel is not supported for images with indexed pixel formats.

What is the alternative to the setpixel function, for an indexed image?

N.b - I managed to use the getpixel to read from the image

Thanks

Jansen

[370 byte] By [tamasu] at [2007-12-28]
# 1

Hi,

If you can convert the gif to a .bmp bitmap file then load the bitmap file then SetPixel should work.

Regards,

S_DS.

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Further to your e-mail, I have converted the file to a bmp using the code]

objBmp.Save(sNewFile, imgFormat)

were imgformat is the bmp format

Confirmed that the file was created and it was readable however it again failed

Dim BMImage As Bitmap = CType(Image.FromFile(StrFileNamePath), Bitmap)

BMImage.SetPixel(Xindex, Yindex, Color.Cyan)

What I am doing wrong?

tamasu at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Sets the color of the specified pixel in this Bitmap object.

[Visual Basic]

Public Sub SetPixel( _

ByVal x As Integer, _

ByVal y As Integer, _

ByVal color As Color _

)

[C#]

public void SetPixel(

int x,

int y,

Color color

);

[C++]

public: void SetPixel(

int x,

int y,

Color color

);

[JScript]

public function SetPixel(

x : int,

y : int,

color : Color

);

Parameters

x

The x-coordinate of the pixel to set.

y

The y-coordinate of the pixel to set.

color

A Color structure that represents the color to assign to the specified pixel.

Return Value

This method does not return a value.

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap object.

  • Sets the color of each pixel in the bitmap to black.

  • Draws the bitmap.
[Visual Basic] 

Public Sub SetPixel_Example(e As PaintEventArgs)

' Create a Bitmap object from a file.

Dim myBitmap As New Bitmap("Grapes.jpg")

' 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

 
I'm not sure if this means you HAVE TO draw your image to the screen first?
Regarding my 1st post and using this code you may be able to load a GIF directly
as this example uses a JPG image. I just thought that as Windows mostly uses BMP files
then you would need to use a BMP.
Regards,
S_DS.
 
Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

tamasu,

You need to convert the indexed image to a non-indexed image. Here is a function to do that, along with a short demo that uses it:

Imports System.Drawing

Module Module1
Function CreateNonIndexedImage(ByVal src As Image) As Bitmap
Dim newBmp As New Bitmap(src.Width, src.Height, Imaging.PixelFormat.Format32bppArgb)

Using gfx As Graphics = Graphics.FromImage(newBmp)
gfx.DrawImage(src, 0, 0)
End Using

Return newBmp
End Function

Sub Main()
' indexed.bmp is a 16-color bmp file
Dim fromFile As Image = Image.FromFile("C:\indexed.bmp")
Dim nonIndexed As Bitmap = CreateNonIndexedImage(fromFile)

nonIndexed.SetPixel(5, 5, Color.Blue)

nonIndexed.Save("C:\non-indexed.bmp")

nonIndexed.Dispose()
fromFile.Dispose()
End Sub

End Module

Basically, you can load the file in any format, then get a 32-bit version of it by calling the CreateNonIndexedImage() function. Then you can call SetPixel on it.

[edit] GIF files are always indexed, so a conversion is unavoidable.

Hope this helps.

Matt

MattDe_MS at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...