C# .NET - Retrieving,Resizin VarBinary() Image in datagrid W form App sqclient

Asked By Rajuje Abbasi
11-Feb-12 04:28 PM
Please tell me now for the,  how to retrieve the VarBinary Image in Window Form application Using SQLCLIENT ? Please tell me a am waiting for you replay help me in this topic as you help me for how to retrieving Image in wpf application . Please please .
Yes i want to re size the Image of type Varbinary and also show it in Datagridview so please tell me i do to retrieve it but problem is that i can re size it So tell me how to do it please please 
  D Company replied to Rajuje Abbasi
12-Feb-12 01:00 AM
Hello,

Simply through code you can do this.like this
//On form load of your application do like this,use all necessary using statements

private void Form1_Load(object sender, EventArgs e)
{
  // Get a datatable from the database, table from  your database
  string strConString= Properties.Settings.Default.ConString;
  using (var cmd= new SqlCommand("select * from yourTableName"))
  {
    var NewTable = LibSQL.GetDataTableFromCommand(cmd, strConString);
//as you have mentioned that you want to bind it it to gridview control
    Yourgridname.DataSource = NewTable;
  }

  // Now u can add the new image column to the grid
  var imgCOloumn= new DataGridViewImageColumn { Name = "SomeImage" };
  Yourgridname.Columns.Add(imgCOloumn);
//here you can customize the size of image put some conditions also if u required
}
}


Hope its clear to you!!
Regards
D
  kalpana aparnathi replied to Rajuje Abbasi
12-Feb-12 05:48 AM
hi,

you want retrieve and resize varbinary image from database into the datagrid with window form for solution purpose i give you one article link so please http://www.codeproject.com/Articles/268123/Display-Store-and-Retrieve-Image-Data-from-Databas

Thanks,
  dipa ahuja replied to Rajuje Abbasi
12-Feb-12 07:03 AM
public bool SaveImage(HttpPostedFile image, string title)
{
  bool result = false;
  int MaxWidth = 500;
  int MaxHeight = 500;
  int ThumbMaxWidth = 150;
  int ThumbMaxHeight = 150;
 
  // Ensure a unique filename
  string upFileName = new Guid().ToString() + image.FileName.Substring(image.FileName.LastIndexOf("."));
 
  // Should put this into a config file.
  string LargeFile = @"UserImages\Large\" + upFileName;
  string ThumbFile = @"UserImages\Thumbs\" + upFileName;
 
  try
  {
    System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(image.InputStream);
    System.Drawing.Image LargeImage = ImageTool.ToMaxSize(UploadedImage, MaxWidth, MaxHeight);
    System.Drawing.Image ThumbImage = ImageTool.ToMaxSize(UploadedImage, ThumbMaxWidth, ThumbMaxHeight);
 
    LargeImage.Save(Request.PhysicalApplicationPath + LargeFile);
    ThumbImage.Save(Request.PhysicalApplicationPath + ThumbFile);
  }
  catch (Exception ex)
  {
    return false;
  }
 
  GalleryDataContext db = new GalleryDataContext();
 
  // Save the image paths and title to the database using LINQ
  GalleryImage i = new GalleryImage();
  i.FullSize = LargeFile.Replace('\\', '/');
  i.Thumbnail = ThumbFile.Replace('\\', '/');
 
  i.Title = title;
 
  try
  {
    db.GalleryImages.InsertOnSubmit(i);
    db.SubmitChanges();
  }
  catch (Exception ex)
  {
    return false;
  }
 
  return result;
}
<http://www.robbihun.com/Post/ASPNET-Uploading-and-Resizing-Images>
 
 
  Rajuje Abbasi replied to dipa ahuja
12-Feb-12 03:43 PM
Thanks to both of you but :
Sorry but both are for ASP not for the Window form Please tell me some thing for window form 
Create New Account
help
into tb_photo(albumID, photoName, login_ID, photocover)Values ( " + ddl_albumname.SelectedValue + ", '" + pathToCheck + "', '" + lbl_username.Text.Trim() + "', '" + ddl_status.SelectedValue + "')" ; SqlCommand cmdMsg = new SqlCommand (mySql, conn); cmdMsg.ExecuteNonQuery(); conn.Close(); / / Check to see if a file already exists with into tb_photo(albumID, photoName, login_ID, photocover)Values ( " + ddl_albumname.SelectedValue + ", '" + pathToCheck + "', '" + lbl_username.Text.Trim() + "', '" + ddl_status.SelectedValue + "')" ; SqlCommand cmdMsg = new SqlCommand (mySql, conn); cmdMsg.ExecuteNonQuery(); conn.Close(); / / Check to see if a file already exists with 150; / / Ensure a unique filename string upFileName = new Guid ().ToString() + image.FileName.Substring(image.FileName.LastIndexOf( "." )); / / Should put this into a config file. string LargeFile = @"UserImages \ Large \ " + upFileName; string ThumbFile = @"UserImages GalleryImage(); i.FullSize = LargeFile.Replace( ' \ ' , ' / ' ); i.Thumbnail = ThumbFile.Replace( ' \ ' , ' / ' ); i.Title = title; try { db.GalleryImages.InsertOnSubmit(i); db.SubmitChanges(); } catch ( Exception ex) { return false ; } return result; } http: / / www.robbihun.com / Post
DataSource = dt; DataList1.DataBind(); } connection.Open(); / / Query to insert images path and name into database SqlCommand cmd = new SqlCommand("Insert into Images(ImageName) values(@ImageName)", connection); / / Passing parameters to query cmd.Parameters.AddWithValue("@ImageName 150; / / Ensure a unique filename string upFileName = new Guid ().ToString() + image.FileName.Substring(image.FileName.LastIndexOf( "." )); / / Should put this into a config file. string LargeFile = @"UserImages \ Large \ " + upFileName; string ThumbFile = @"UserImages GalleryImage(); i.FullSize = LargeFile.Replace( ' \ ' , ' / ' ); i.Thumbnail = ThumbFile.Replace( ' \ ' , ' / ' ); i.Title = title; try { db.GalleryImages.InsertOnSubmit(i); db.SubmitChanges(); } catch ( Exception ex) { return false ; } return result; } <http: / / www.robbihun.com / Post
into tb_photo(albumID, photoName, login_ID, photocover)Values ( " + ddl_albumname.SelectedValue + ", '" + pathToCheck + "', '" + lbl_username.Text.Trim() + "', '" + ddl_status.SelectedValue + "')" ; SqlCommand cmdMsg = new SqlCommand (mySql, conn); cmdMsg.ExecuteNonQuery(); conn.Close(); / / Check to see if a file already exists with 150; / / Ensure a unique filename string upFileName = new Guid ().ToString() + image.FileName.Substring(image.FileName.LastIndexOf( "." )); / / Should put this into a config file. string LargeFile = @"UserImages \ Large \ " + upFileName; string ThumbFile = @"UserImages GalleryImage(); i.FullSize = LargeFile.Replace( ' \ ' , ' / ' ); i.Thumbnail = ThumbFile.Replace( ' \ ' , ' / ' ); i.Title = title; try { db.GalleryImages.InsertOnSubmit(i); db.SubmitChanges(); } catch ( Exception ex) { return false ; } return result; } http: / / www.robbihun.com / Post
using(sqlcommand cmd = new sqlcommand) hii all, can u tel me the difference between sqlcommand cmd = new sqlcommand(); and using(sqlcommand cmd = new sqlcommand()); same as for sqldataadapter also hi, using(SqlCommand cmd = new SqlCommand()) { . . . } is equivalent to writing, SqlCommand cmd = new SqlCommand(); try { . . . } finally { cmd.Dispose(); } Object.Dispose() Actually