Click here to Skip to main content
15,896,437 members
Home / Discussions / C#
   

C#

 
Questioncode problem Pin
bhatted15-Dec-08 15:51
bhatted15-Dec-08 15:51 
AnswerRe: code problem Pin
Christian Graus15-Dec-08 16:31
protectorChristian Graus15-Dec-08 16:31 
GeneralRe: code problem Pin
bhatted16-Dec-08 5:06
bhatted16-Dec-08 5:06 
QuestionDisplaying Graphics in Windows Form Pin
ejohns8515-Dec-08 9:33
ejohns8515-Dec-08 9:33 
GeneralRe: Displaying Graphics in Windows Form Pin
Luc Pattyn15-Dec-08 9:45
sitebuilderLuc Pattyn15-Dec-08 9:45 
JokeRe: Displaying Graphics in Windows Form Pin
DaveyM6915-Dec-08 10:20
professionalDaveyM6915-Dec-08 10:20 
GeneralRe: Displaying Graphics in Windows Form Pin
Dave Kreskowiak15-Dec-08 10:49
mveDave Kreskowiak15-Dec-08 10:49 
AnswerRe: Displaying Graphics in Windows Form [modified] Pin
Think-A-Tron15-Dec-08 23:31
Think-A-Tron15-Dec-08 23:31 
Using SetPixel is dog slow. If more than a few pixels are modified, it's next to worthless. I know of two efficient ways to modify a bitmap on a pixel-by-pixel basis. Both methods have advantages.

First, lock the bitmap, access it with pointers in unsafe mode, then unlock it. Here's a link to a very nice article using this approach:

Code Project Article

Second, lock the bitmap, copy it to a regular array, and unlock it. Modify the data in the array, and when done, lock the bitmap, copy the data back, then unlock the bitmap. If you only want to initialize the bitmap, you can simplify the process, since there's no need to copy from the bitmap. Just write the regular array and copy it to the (locked) bitmap. This idea is also useful if you only want to individually modify pixels, and don't need to use any of the bitmap features, such as line drawing. Use the pixel array as the "master" version, and copy it into the bitmap to display it. Here's an example of this method:
<br />
// (Some of these may not be necessary.)<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
using System.Drawing.Imaging;<br />
<br />
    // Could use enumeration here.<br />
    const int bmB = 0;<br />
    const int bmG = 1;<br />
    const int bmR = 2;<br />
    const int bmA = 3;<br />
   <br />
    //<br />
    // Allocate a pixel array to match the bitmap.<br />
    //<br />
    int bitsPerPixel;<br />
    bool hasAlpha;<br />
    switch (bitmap.PixelFormat)<br />
    {<br />
        case PixelFormat.Format24bppRgb:<br />
	    bitsPerPixel = 24;<br />
            hasAlpha = false;<br />
	    break;<br />
        case PixelFormat.Format32bppRgb:<br />
            bitsPerPixel = 32;<br />
            hasAlpha = false;<br />
        case PixelFormat.Format32bppArgb:<br />
        case PixelFormat.Format32bppPArgb:<br />
            bitsPerPixel = 32;<br />
            hasAlpha = true;<br />
            break;<br />
        default:<br />
            throw new ArgumentException("Unsupported pixel format.");<br />
    }<br />
<br />
    // (Stride is available in the BitmapData structure.)<br />
    int bytesPerPixel = bitsPerPixel >> 3;<br />
    int stride = ((bitmap.Width * bitsPerPixel + 31) & ~31) >> 3;<br />
    int bitmapBytes = stride * bitmap.Height;<br />
    byte[] pixels = new byte[bitmapBytes];<br />
<br />
...<br />
    //<br />
    // Copy the bitmap into the pixel array.<br />
    //<br />
<br />
    // Lock the bitmap's bits.<br />
    BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);<br />
<br />
    // Copy the bitmap pixels into the array.<br />
    Marshal.Copy(bmpData.Scan0, pixels, 0, bitmapBytes);<br />
<br />
    // Unlock the bits.<br />
    bitmap.UnlockBits(bmpData);<br />
<br />
...<br />
    //<br />
    // Copy the pixel array into the bitmap.<br />
    //<br />
<br />
    // Lock the bitmap's bits.  <br />
    BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);<br />
<br />
    // Copy the array back into the bitmap.<br />
    Marshal.Copy(pixels, 0, bmpData.Scan0, bitmapBytes);<br />
<br />
    // Unlock the bits.<br />
    bitmap.UnlockBits(bmpData);<br />
	<br />

Note this only works for direct (non-indexed) bitmaps. For 24 bit pixels, the pixels are stored in B, G, R order; for 32 bit pixels, the order is B, G, R, A. For example, to access the green component of a 24 bit pixel at (x, y), use pixels[(bytesPerPixel * x + stride * y) + bmG].

modified on Tuesday, December 16, 2008 6:20 PM

AnswerRe: Displaying Graphics in Windows Form Pin
User 665815-Dec-08 9:45
User 665815-Dec-08 9:45 
Questiongetting a file type's icon location (SHGetFileInfo??) Pin
mirko8615-Dec-08 8:30
mirko8615-Dec-08 8:30 
AnswerRe: getting a file type's icon location (SHGetFileInfo??) Pin
Giorgi Dalakishvili15-Dec-08 8:45
mentorGiorgi Dalakishvili15-Dec-08 8:45 
GeneralRe: getting a file type's icon location (SHGetFileInfo??) Pin
mirko8615-Dec-08 8:59
mirko8615-Dec-08 8:59 
QuestionDateTime Parse. Pin
postonoh15-Dec-08 6:21
postonoh15-Dec-08 6:21 
AnswerRe: DateTime Parse. Pin
Wendelius15-Dec-08 6:39
mentorWendelius15-Dec-08 6:39 
GeneralRe: DateTime Parse. [modified] Pin
postonoh15-Dec-08 6:47
postonoh15-Dec-08 6:47 
GeneralRe: DateTime Parse. Pin
Wendelius15-Dec-08 6:52
mentorWendelius15-Dec-08 6:52 
QuestionSleep thread but not current Pin
Xmen Real 15-Dec-08 6:00
professional Xmen Real 15-Dec-08 6:00 
AnswerRe: Sleep thread but not current Pin
User 665815-Dec-08 6:38
User 665815-Dec-08 6:38 
GeneralRe: Sleep thread but not current Pin
Xmen Real 15-Dec-08 6:43
professional Xmen Real 15-Dec-08 6:43 
GeneralRe: Sleep thread but not current Pin
User 665815-Dec-08 6:48
User 665815-Dec-08 6:48 
GeneralRe: Sleep thread but not current Pin
Xmen Real 15-Dec-08 6:50
professional Xmen Real 15-Dec-08 6:50 
GeneralRe: Sleep thread but not current Pin
User 665815-Dec-08 6:52
User 665815-Dec-08 6:52 
GeneralRe: Sleep thread but not current Pin
Luc Pattyn15-Dec-08 6:45
sitebuilderLuc Pattyn15-Dec-08 6:45 
GeneralRe: Sleep thread but not current Pin
Xmen Real 15-Dec-08 6:48
professional Xmen Real 15-Dec-08 6:48 
GeneralRe: Sleep thread but not current Pin
User 665815-Dec-08 6:51
User 665815-Dec-08 6:51 

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.