C# & COM & Excel: How to Insert Image


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: C# & COM & Excel: How to Insert Image

Hybrid View

  1. #1
    Join Date
    Jun 2006
    Posts
    10

    Question C# & COM & Excel: How to Insert Image into cell

    I've mananged to successfully use COM to open up Microsoft Excel and insert an image for each row of data based on the filename retrieved from my database.

    I am using code found below, which I found on Google:
    Code:
     string filename = "C:\\PHOTOS\\" + reader["photo_no"] + ".JPG";
                   FileInfo fi = new FileInfo(filename);  
                        
                   if (fi.Exists){
    
                        m_oSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse,
                       Microsoft.Office.Core.MsoTriState.msoCTrue, 1, 200, 40, 50);
                    }
    The problem is that the AddPicture funtion uses X,Y coordinates to place the picture. So it's hard and messy to place the picture exactly in the cell that you want.

    Is there is a function where I can place the picture right into the exact cell by using the range function?


    Edit: I would like to point out there ideally I would like to use this code:

    Code:
    ThisWorkBook = ThisApplication.Workbooks.Open(_stFileName,missing,missing,5,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); 
    
    Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1]; 
    Excel.Range oRange = (Excel.Range)ThisSheet.Cells[10,1]; 
    
    Image oImage = Image.FromFile(_stLOGO); 
    System.Windows.Forms.Clipboard.SetDataObject(oImage,true); 
    ThisSheet.Paste(oRange,_stLOGO);
    But, the problem with this code is that YOU CANNOT SPECIFIY the image size. So if your original JPG is 1000x1000 then that's how big it'll look once you pasted into Excel.


    Thanks again. Hope this helps a few others out there.
    Last edited by mav8; 06-29-2006 at 04:31 AM.

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    I think you might change the original JPG (1000x1000) to Thumbnail Image before setting the object into the clipboard.

    eg:

    Code:
    Image oImage = Image.FromFile(_stLOGO); 
    oImage = = oImage.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
    System.Windows.Forms.Clipboard.SetDataObject(oImag  e,true); 
    ThisSheet.Paste(oRange,_stLOGO);
    Code:
     public bool ThumbnailCallback() {
           return false;
     }
    Ref : How To: Creating Thumbnail Images
    http://www.csharp-station.com/Articles/Thumbnails.aspx

    Hopefully you may find it useful.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Jun 2006
    Posts
    10

    Talking

    Thanks Sync...

    For the last 3 hours, I think I googled all the webpages regarding C#, thumbnails and images.

    For my application, I need the thumbnail to be in PROPORTION TO THE ORIGINAL IMAGE. The thumbnail function doesnt do that

    So, I decided to use this guy's code, which I found on another forum:

    Code:
     string FileToResize = "C:\\PLANTASIA_CALC\\PHOTOS\\" + photo_no + ".JPG";
                string newFilename = "C:\\PLANTASIA_CALC\\THUMBS\\" + photo_no + ".JPG";
    
    
                using (Bitmap originalBitmap = Bitmap.FromFile(FileToResize, true) as Bitmap, newbmp = new Bitmap(80, 80))
                {
                    double WidthVsHeightRatio = (float)originalBitmap.Width / (float)originalBitmap.Height;
    
                    using (Graphics newg = Graphics.FromImage(newbmp))
                    {
                        newg.Clear(Color.White); newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        if (WidthVsHeightRatio == 1d)
                        {
                            newg.DrawImage(originalBitmap, 0, 0, 80, 80);
                            newg.Save();
                        }
    
                        else if (WidthVsHeightRatio < 1d) //Image is taller than wider
                        {
                            newg.DrawImage(originalBitmap, new RectangleF(new PointF((float)(40 - ((80 * WidthVsHeightRatio) / 2)), 0), new SizeF((float)(80 * WidthVsHeightRatio), 80f)));
                            newg.Save();
                        }
    
                        else //Image is wider than taller
                        {
                            double inverse = Math.Pow(WidthVsHeightRatio, -1);
                            newg.DrawImage(originalBitmap, new RectangleF(new PointF(0, (float)(40 - ((80 * inverse) / 2))), new SizeF(80f, (float)(80 * inverse))));
                            newg.Save();
                        }
                    }
    
                    newbmp.Save(newFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
    Credits to him: works great for me. The numbers "80" and "40"(half of 80) are variables so if anyone wants to reuse the code they can.


    THANKS ALOT SYNC AGAIN!!

  4. #4
    Join Date
    Apr 2013
    Posts
    1

    excel picture insert

    in a simple way you can do this
    Code:
    xlWorkSheet.Shapes.AddPicture("C:\\csharp-xl-picture.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);
    source :

    http://csharp.net-informations.com/e...ture-excel.htm

    flaming.

  5. #5
    Join Date
    Apr 2013
    Posts
    1
    for image processing, i think an processing tool is obviously indispensiable, you can just download one tool from the internet and have a try. good luck !

Similar Threads

  1. how to clone database using mysql
    By zarien in forum Database
    Replies: 1
    Last Post: 05-17-2006, 04:27 AM
  2. Replies: 1
    Last Post: 01-26-2006, 11:54 PM
  3. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM
  4. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 07:24 PM
  5. Replies: 0
    Last Post: 04-06-2000, 07:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center