473,382 Members | 1,442 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,382 software developers and data experts.

Image to raw results with weird error

OGG
Hi guys,

I am trying to do a simple task: convert bitmap to raw and back to
bitmap as fast as possible.
Here are the 2 methods I am using:

private byte[] ToRaw(Bitmap bmp)
{
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat);
byte[] buffer = new byte[data.Height * data.Width * 3];
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
bmp.UnlockBits(data);
return buffer;
}

private Bitmap ToBitmap(byte[] raw, int w, int h, PixelFormat
pf)
{
Bitmap bmp = new Bitmap(w, h, pf);
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadWrite, pf);
Marshal.Copy(raw, 0, data.Scan0, raw.Length);
bmp.UnlockBits(data);
return bmp;
}
It works perfectly to any image with width less than 700 pixels or
more than 799 pixels.
The strangest thing happens to a bitmap with width of 701 to 799. It
simply returns the image a little distorted, and I have no clue why.

BTW, if I use simple bitmap.GetPixel / SetPixel it works, but it takes
a lot of time.

Any idea is appreciated.

Thanks.

Aug 22 '07 #1
1 1504
The width of a row of pixels is not necessarily the width of the row in
bytes. The actual byte width of the row is stored in the BitmapData.Stride
property. So, you need to use that to read and write the the Bitmap. If you
don't want the (possible) padding in the row, you can use the Stride minus
the Width to get the size of the difference. Then you can use that
difference to write the bytes back to the Bitmap.

See http://www.bobpowell.net/lockingbits.htm for more information.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"OGG" <it**********@gmail.comwrote in message
news:11*********************@z24g2000prh.googlegro ups.com...
Hi guys,

I am trying to do a simple task: convert bitmap to raw and back to
bitmap as fast as possible.
Here are the 2 methods I am using:

private byte[] ToRaw(Bitmap bmp)
{
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadOnly, bmp.PixelFormat);
byte[] buffer = new byte[data.Height * data.Width * 3];
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
bmp.UnlockBits(data);
return buffer;
}

private Bitmap ToBitmap(byte[] raw, int w, int h, PixelFormat
pf)
{
Bitmap bmp = new Bitmap(w, h, pf);
BitmapData data = bmp.LockBits(new Rectangle(new Point(0,
0), bmp.Size), ImageLockMode.ReadWrite, pf);
Marshal.Copy(raw, 0, data.Scan0, raw.Length);
bmp.UnlockBits(data);
return bmp;
}
It works perfectly to any image with width less than 700 pixels or
more than 799 pixels.
The strangest thing happens to a bitmap with width of 701 to 799. It
simply returns the image a little distorted, and I have no clue why.

BTW, if I use simple bitmap.GetPixel / SetPixel it works, but it takes
a lot of time.

Any idea is appreciated.

Thanks.

Aug 22 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: pescott | last post by:
Hello people, I could really use your help on this one... I have script to upload image data to a database, but all that is uploaded is the decription, not the data BLOB itself. There must be...
3
by: CanyonJ | last post by:
I've been running into a frustrating problem for a while now. I use image.FromStream to open a tif, then perform some drawstring methods, and then save the file again. The tiffs are in 1 bit per...
3
by: christiang | last post by:
Hi guys, I'd like to use Alexia thumbnail service. It's an interesting service, you pass an url to its cgi and you get a thumbnail of that url. Of course they don't have the screenshots of all the...
2
by: Aleksandar Cikota | last post by:
Hi all, I have a problem with openning of an image. Here is the Code: from Tkinter import * from PIL import Image, ImageTk from win32com.client import gencache import tkMessageBox
4
by: escristian | last post by:
Hello. I'm trying to create an Image so I use something like this: Image newImage = Image.FromFile(filename); Now when it's a bmp file and certain .gif files it gives me an exception that...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
5
by: MattC | last post by:
Hi, I have a webservice that returns a byte for an image. I'd like to disaplay an image while the webservice is being contacted and then once the byte is available show that and remove the...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
0
by: cewood | last post by:
Hi, I was hoping someone could help me with the following: I'm writing code to try and get a image from a USB camera mfg. dll. Here's the dll function: SDK_API MTUSB_GetCurrentFrame(...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.