473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

My image is cut when writing to a bitmap!

Hello everyone!

I currently write a program which indexes an image to 16 colors, correcting its background-color and correcting the image itself afterwards. To "write pixels" I coded a little method:

Expand|Select|Wrap|Line Numbers
  1. void SetPixel(Bitmap bmp, int x, int y, Color c)
  2.             int i;
  3.             int min = int.MaxValue;
  4.             int minI = 0;
  5.             for (i = 0; i < 256; i++)
  6.             {
  7.                 Color cPal = bmp.Palette.Entries[i];
  8.                 int r = c.R - cPal.R;
  9.                 int g = c.G - cPal.G;
  10.                 int b = c.B - cPal.B;
  11.                 int d = r * r + g * g + b * b;
  12.                 if (d == 0) break;
  13.                 if (d < min)
  14.                 {
  15.                     min = d;
  16.                     minI = i;
  17.                 }
  18.             }
  19.             if (i == 256) i = minI;
  20.             BitmapData bmpData = bmp.LockBits(new Rectangle(x, y, 1, 1),
  21.                                               ImageLockMode.ReadOnly,
  22.                                               bmp.PixelFormat);
  23.             Marshal.WriteByte(bmpData.Scan0, (byte)i);
  24.             bmp.UnlockBits(bmpData);
The output is this:



It works with 8bpp images but not with 4bpp! help :(
As you can see, the image is kind of cut every 2nd pixel.
(Note: Left one is INPUT; right is OUTPUT)
Mar 22 '14 #1
0 1148

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nick | last post by:
How to change the pic to a "show error" image when loading images failed - for example, no image file exists? I tried to add the following statement in the onload event function: if...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
5
by: Stan Shankman | last post by:
C# -- Visual Studio.Net – Windows Application Greetings all, How do I copy a Form’s image to a bitmap? – And do so before it gets to the screen. I haven’t been able to find anyone that knows...
0
by: Panic Student | last post by:
hi all, i'm having trouble with changing image when mouseover on a particular label. Below is what i attempted to but it didn't seem to work at all. I guess my concept is wrong ): Please...
3
by: aledrjones | last post by:
Hi I've got a c# application that will often run in the background as it is used to monitor connections to a host. An activex control displays the communication between the app and the host. ...
5
by: canajien | last post by:
I am trying to get a script to show a particular image when a field value = 1, otherwise it will show something else, and this is what I came up with: $i=0; while ($i < $num) { ...
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
9
by: ghjk | last post by:
change the background image when Button Click ====================================== I' developing site with php and postgres. It has menus list in the left side. and all are images(jpg) EX:Add...
3
by: tess1243 | last post by:
I want the image to be changed when it is hovered, and display the changed image on the hovered image. I want the changed image exactly to display on the hovered image. Here is the code: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.