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

image concatenation in c#

Hi,
I haven't had much luck searching for guidance on the following
challenge.
I have a .NET app in which I would like to take multiple thumnail
images and combine them into one flat image.
Could someone give me an example of how to take two images, imgA and
imgB and create imgC which would be one image of imgA and imgB side by
side or imgA on top of imgB. If both imgA and imgB are 10pixels x 10
pixels, the side by side version of imgC would be 10pixels x 20 pixels.
Thanks in advance.

Jon

May 18 '06 #1
10 5735
First, you create an image that is 10X20 pixels in size, and get the
Graphics from it to draw on it. Then you draw the first image on one half,
and the second on the other. For details, see the System.Drawing namespace:

http://msdn.microsoft.com/library/en...asp?frame=true

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi,
I haven't had much luck searching for guidance on the following
challenge.
I have a .NET app in which I would like to take multiple thumnail
images and combine them into one flat image.
Could someone give me an example of how to take two images, imgA and
imgB and create imgC which would be one image of imgA and imgB side by
side or imgA on top of imgB. If both imgA and imgB are 10pixels x 10
pixels, the side by side version of imgC would be 10pixels x 20 pixels.
Thanks in advance.

Jon

May 18 '06 #2
Thanks,
Could you point me to a code example? I've looked around for a day now
and haven't found the solution. I've seen examples of overlaying
watermarks on images, but not taking two images and "gluing them
together" to make one.

thanks again.

Jon

May 18 '06 #3
I don't know where to find a code sample. It's pretty straightforward. Let's
say you have 2 images, each is 10X10 pixels in size. You want to put
"imageA" on the left, and "imageB" on the right. You create a 20X10 image,
get the Graphics from it, and draw each image at its original size. The only
difference is that you draw "imageA" at location (0,0) and "imageB" at
location (10,0).

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Thanks,
Could you point me to a code example? I've looked around for a day now
and haven't found the solution. I've seen examples of overlaying
watermarks on images, but not taking two images and "gluing them
together" to make one.

thanks again.

Jon

May 18 '06 #4
Here's the code I'm trying:

System.Drawing.Graphics g;
System.Drawing.Bitmap newImg = new Bitmap(600, 200);
System.Drawing.Image img1 =
System.Drawing.Image.FromFile(Server.MapPath("./images/") +
"airport_r1_c2.gif");
System.Drawing.Image img2 =
System.Drawing.Image.FromFile(Server.MapPath("./images/") +
"airport_r2_c2_f2.gif");
g = System.Drawing.Graphics.FromImage(newImg);
g.DrawImage(img1,0,0);
g.DrawImage(img2, 281, 0);
g.Save();
newImg.Save(Server.MapPath("./images/") + "NewBitmap.jpg");
System.Runtime.InteropServices.ExternalException was unhandled by user
code
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo
encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat
format)
at System.Drawing.Image.Save(String filename)
at Default2.Page_Load(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\Test\Default2.aspx.cs:line 28
at System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp,
Object o, Object t, EventArgs e)
at
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender,
EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

May 18 '06 #5
What are the actual dimensions of both images? Considering the fact that
these images are different image types than the Bitmap you're drawing to, I
would suggest using the overload that takes a Rectangle instead of an X and
a Y.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Here's the code I'm trying:

System.Drawing.Graphics g;
System.Drawing.Bitmap newImg = new Bitmap(600, 200);
System.Drawing.Image img1 =
System.Drawing.Image.FromFile(Server.MapPath("./images/") +
"airport_r1_c2.gif");
System.Drawing.Image img2 =
System.Drawing.Image.FromFile(Server.MapPath("./images/") +
"airport_r2_c2_f2.gif");
g = System.Drawing.Graphics.FromImage(newImg);
g.DrawImage(img1,0,0);
g.DrawImage(img2, 281, 0);
g.Save();
newImg.Save(Server.MapPath("./images/") + "NewBitmap.jpg");
System.Runtime.InteropServices.ExternalException was unhandled by user
code
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo
encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat
format)
at System.Drawing.Image.Save(String filename)
at Default2.Page_Load(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\Test\Default2.aspx.cs:line 28
at System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp,
Object o, Object t, EventArgs e)
at
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender,
EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

May 18 '06 #6
The dimensions of each are 280 x 17. I was making the new bitmap large
enough to accomodate them plus several others.
Thanks,
Jon

May 18 '06 #7
Make the Bitmap large enough to accomodate exactly what you need to draw.
Let me know if the Rectangle solution works for you. It will resize the
image to fit in the Rectangle.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
The dimensions of each are 280 x 17. I was making the new bitmap large
enough to accomodate them plus several others.
Thanks,
Jon

May 18 '06 #8
Hi,
I've had some success, however the placement of images is still not
working as I think it should. In this example, I'm taking one image,
SlotLarge.jpg and am placing it twice into a new image. The file is 48
x 67. I'm placing the first instance at (0, 0) and the second at (0,
68), so they should be one over the other. What's happening though is
the second one is overlapping the first one. You can see the files at:
http://www.byrd48.net/imagetest.htm

The code is below. Thanks again.

m_fileDir =
@"c:\inetpub\wwwroot\pfs\images\machines\45countla rge\";

m_Bitmap = new Bitmap(320, 600);
Graphics g = Graphics.FromImage(m_Bitmap);

System.Drawing.Image _img =
System.Drawing.Image.FromFile(m_fileDir + "SlotLarge.jpg");
g.DrawImage(_img, 0, 0);

g.DrawImage(_img, 0, 68);

m_Bitmap.Save(m_fileDir + "testimage.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);

May 24 '06 #9
It is important to follow instructions carefully. Do you remember when I
recommended that you use the overload of the DrawImage method that takes a
destination rectangle? Your image is being drawn larger than it is
originally. I mentioned that, due to the different image formats, the image
scaling might be necessary. Also, if you had only made the area exactly the
size you need to draw to (also recommended earlier), you would have seen
immediately that the image was not the same size in the bitmap.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi,
I've had some success, however the placement of images is still not
working as I think it should. In this example, I'm taking one image,
SlotLarge.jpg and am placing it twice into a new image. The file is 48
x 67. I'm placing the first instance at (0, 0) and the second at (0,
68), so they should be one over the other. What's happening though is
the second one is overlapping the first one. You can see the files at:
http://www.byrd48.net/imagetest.htm

The code is below. Thanks again.

m_fileDir =
@"c:\inetpub\wwwroot\pfs\images\machines\45countla rge\";

m_Bitmap = new Bitmap(320, 600);
Graphics g = Graphics.FromImage(m_Bitmap);

System.Drawing.Image _img =
System.Drawing.Image.FromFile(m_fileDir + "SlotLarge.jpg");
g.DrawImage(_img, 0, 0);

g.DrawImage(_img, 0, 68);

m_Bitmap.Save(m_fileDir + "testimage.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);

May 24 '06 #10
Thanks,
As it turns out, there are methods with width and height parameters, so
all I had to do was add them:

instead of: g.DrawImage(_img, 0, 0);

it should have been: g.DrawImage(_img, 0, 0, w, h);
Kevin Spencer wrote:
It is important to follow instructions carefully. Do you remember when I
recommended that you use the overload of the DrawImage method that takes a
destination rectangle? Your image is being drawn larger than it is
originally. I mentioned that, due to the different image formats, the image
scaling might be necessary. Also, if you had only made the area exactly the
size you need to draw to (also recommended earlier), you would have seen
immediately that the image was not the same size in the bitmap.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"byrd48" <by*****@rocketmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi,
I've had some success, however the placement of images is still not
working as I think it should. In this example, I'm taking one image,
SlotLarge.jpg and am placing it twice into a new image. The file is 48
x 67. I'm placing the first instance at (0, 0) and the second at (0,
68), so they should be one over the other. What's happening though is
the second one is overlapping the first one. You can see the files at:
http://www.byrd48.net/imagetest.htm

The code is below. Thanks again.

m_fileDir =
@"c:\inetpub\wwwroot\pfs\images\machines\45countla rge\";

m_Bitmap = new Bitmap(320, 600);
Graphics g = Graphics.FromImage(m_Bitmap);

System.Drawing.Image _img =
System.Drawing.Image.FromFile(m_fileDir + "SlotLarge.jpg");
g.DrawImage(_img, 0, 0);

g.DrawImage(_img, 0, 68);

m_Bitmap.Save(m_fileDir + "testimage.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);


Jun 15 '06 #11

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

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
3
by: Bill Brother | last post by:
Is it possible to retrieve the local full path of an image ( not the url )?
23
by: John | last post by:
Last year, I remember finding a web page describing how to pass the name of a file to another web page, and have that web page load that image file. Now, I can't find my record of that (it was...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
5
by: Camet | last post by:
I have been trying to create a dynamic id for a number of images in a table so that I can identify which image was clicked on later. ie I need to set a variable to the id of each image, that is...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.