locked
Split and combine image RRS feed

  • Question

  • User-1664238401 posted

    I'm trying to split the image using C# but it was unsuccessful, I need a master to guide me correct the below codes. thank you in advance! then how to code for combining the image after splitting the image 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace separateImg
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Bitmap originalImage = new Bitmap(Image.FromFile(@"C:\Users\user pc\Downloads\Spiderman2.jpg"));
    			Rectangle rect = new Rectangle(0, 0, originalImage.Width / 4, originalImage.Height);
    			Bitmap firstHalf = originalImage.Clone(rect, originalImage.PixelFormat);
    			firstHalf.Save(@"C:\Users\user pc\Downloads\Spider1.jpg");
    			rect = new Rectangle((originalImage.Width / 4) * 2, 0, originalImage.Width / 4, originalImage.Height);
    			Bitmap secondHalf = originalImage.Clone(rect, originalImage.PixelFormat);
    			secondHalf.Save(@"C:\Users\user pc\Downloads\Spider2.jpg");
    			rect = new Rectangle((originalImage.Width / 4) * 3, 0, originalImage.Width / 4, originalImage.Height);
    			Bitmap thirdHalf = originalImage.Clone(rect, originalImage.PixelFormat);
    			thirdHalf.Save(@"C:\Users\user pc\Downloads\Spider3.jpg");
    			rect = new Rectangle((originalImage.Width / 4) * 4, 0, originalImage.Width / 4, originalImage.Height);
    			Bitmap fourthHalf = originalImage.Clone(rect, originalImage.PixelFormat);
    			fourthHalf.Save(@"C:\Users\user pc\Downloads\Spider4.jpg");
    		}
    	}
    }
    Monday, December 18, 2017 7:39 AM

All replies

  • User753101303 posted

    Hi,

    Never post about a problem without telling what it is. For now it seems to me the issue could be that you are using 2, 3, 4 multipliers when it should be 1, 2, 3.

    (ie my guess is that Spider1 is ok, Spider2 and Spider3 is the part later than what you expect and if spider4 doesn't fil, it doesn't show anything?)<sub></sub><sup></sup>

    Tuesday, December 19, 2017 8:23 AM
  • User-1664238401 posted

    The question is, I have a picture named Spiderman, 5000x5000 pixel, then <g class="gr_ gr_100 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="100" data-gr-id="100">i</g> need to split the image into 4, after splitting settle, the result of 4 pictures need to be combining to be 1 picture using c#. Thank you. 

    Tuesday, December 19, 2017 3:10 PM
  • User753101303 posted

    And so you tried 0, 1, 2, 3 multipliers rather 0, 2, 3, 4 ?

    My guess is that Spider1 is ok, Spider2 is what you should have for Spider3 and Spider3 what you should have for Spider4 and Spider4 is empty as it starts from "Width" and goes to "Width+Width/4".

    Tuesday, December 19, 2017 4:11 PM
  • User-1664238401 posted

    Yes, <g class="gr_ gr_192 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="192" data-gr-id="192">i</g> tried that way, but it still wronged splitter 

    Tuesday, December 19, 2017 4:21 PM
  • User-1664238401 posted

    finally, I got the answer, thank god! thanks, Mr! <g class="gr_ gr_175 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="175" data-gr-id="175">i</g> try 0,1,2,3 and its succeed 

    Tuesday, December 19, 2017 4:29 PM
  • User-1664238401 posted

     How to combine this 4 pictures after had been split? here my codes, check my codes please, thank you

           String <g class="gr_ gr_73 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="73" data-gr-id="73">finalImage</g> = @"C:\User\user pc\Download\FinalImage.jpg";

                    String jpg0 = @"C:\User\user pc\Download\Spider1.jpg";

                    String jpg1 = @"C:\User\user pc\Download\Spider2.jpg";

                    String jpg2 = @"C:\User\user pc\Download\Spider3.jpg";

                    String jpg3 = @"C:\User\user pc\Download\Spider4.jpg";

                    Image img0 = Image.FromFile(jpg0);

                    Image img1 = Image.FromFile(jpg1);

                    Image img2 = Image.FromFile(jpg2);

                    Image img3 = Image.FromFile(jpg3);

                    int width = img0.Width + img1.Width + img2.Width + img3.Width;

                    int height = img0.Height + img1.Height + img2.Height + img3.Height;

                    Bitmap imgCompleted = new Bitmap(width, height);

                    Graphics g = Graphics.FromImage(imgCompleted);

                    g.Dispose();

                    img0.Dispose();

                    img1.Dispose();

                    img2.Dispose();

                    img3.Dispose();

                    imgCompleted.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);

                    imgCompleted.Dispose();

                    imageLocation.Image = Image.FromFile(finalImage);

    Tuesday, December 19, 2017 6:37 PM
  • User753101303 posted

    Hi,

    You can use https://msdn.microsoft.com/en-us/library/8b0tzkb5(v=vs.110).aspx to draw each image to the target bitmap (imgCompleted) at the proper location using its current size.

    Not sure how general it needs to be but it seems to me the Height you want is the same for all images ie int height=img0.Height; would be enough. Currently you add the height of all images as you have done for the width (for which is is correct).

    Once it works, later you could consider to use https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement to create a code block is disposing images at the end (as well as if an exception happens).

    Tuesday, December 19, 2017 7:01 PM