Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
I want to slice up an image into 256x256 tiles and save out each tile as a jpg file to SQL-Database, I have taken input from user.
Can anybody tell me how do I get equal size slices(just like a checkerbox) of input images.

//button add data to dataGridView
//insert image from pictureBox to dataGridView
private void btn_Add_Click(object sender, EventArgs e)
{
        MemoryStream ms = new MemoryStream();
        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
        byte[] img = ms.ToArray();
        dataGridView1.Rows.Add(txt_UserID.Text, txt_Name.Text, img);

        Bitmap bm = new Bitmap(@" ");
        int w = bm.Width / 2;
        int h = bm.Height / 3;
        Rectangle rTL = new Rectangle(0, 0, w, h);
        Rectangle rBR = new Rectangle(w, h, w, h);
        Bitmap bmTL = (Bitmap)bm.Clone(rTL, bm.PixelFormat);
        Bitmap bmBR = (Bitmap)bm.Clone(rBR, bm.PixelFormat);

    }

//browse image in pictureBox1 Click
private void pictureBox1_Click(object sender, EventArgs e)
{
    OpenFileDialog opf = new OpenFileDialog();
    opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
    if (opf.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = Image.FromFile(opf.FileName);
    }
}
Posted
Updated 17-May-15 8:27am
v3
Comments
Sergey Alexandrovich Kryukov 17-May-15 11:05am    
Define "slice". What's the problem?
—SA
Member 11657542 17-May-15 11:10am    
just like to tear an image in equal parts.

You work with images, not PictureBox, which is merely a (redundant) control used to show an image. To extract some rectangular part of the image, you can use the methods System.Drawing.Graphics.DrawImage:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Don't use the methods which expect the size of new image, to avoid image resampling. Other methods will copy the image part as is.

Draw on what? Well, on any valid instance of System.Drawing.Graphics, but probably you would need to draw on another bitmap. Then create an empty bitmap of required size and get a Graphics instance to draw on it:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage(v=vs.110).aspx[^].

As to storing the images in database, it would be too bad to answer the question which have been answered so many times again. Please see: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap+OR+bitmaps%29&doctypeid=1%3b3%3b14%3b5[^].

It's not always good to store images in a database. It's often better to store only the image names and store the images themselves in the file system. This is usually the better option for Web sites.


—SA
 
Share this answer
 
v2
Use the Bitmap.Clone method:
C#
Bitmap bm = new Bitmap( @"D:\Temp\MyPic.jpg" );
int w = bm.Width / 2;
int h = bm.Height / 3;
Rectangle rTL = new Rectangle( 0, 0, w, h );
Rectangle rBR = new Rectangle( w, h, w, h );
Bitmap bmTL = (Bitmap)bm.Clone( rTL, bm.PixelFormat );
Bitmap bmBR = (Bitmap)bm.Clone( rBR, bm.PixelFormat );
 
Share this answer
 
Comments
Member 11657542 17-May-15 11:40am    
image is from user input so how I pass this address?
Bitmap bm = new Bitmap( @"D:\Temp\MyPic.jpg" );
OriginalGriff 17-May-15 12:01pm    
Just create the image in the usual way - that line is just an example.
How would you normally use the user input?
Member 11657542 17-May-15 13:32pm    
for instance I have tried it on static image how do I save these several sliced files in sql.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900