Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

Excel Chart black and white (Clipboard Copy)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jörgen Ahrens - 08 Mar 2007 08:22 GMT
Hi all

We are creating a chart in an excel app that we then copy to clipboard using
CopyPicture(Screen, xlBitmap, Screen) (interop). Everything works fine when
we set the excelApp.Visible = true; The chart is created and copied into the
clipboard. When we get it
from the clipboard its colorfull und good. But when we set excelApp.Visible
= false and then create the chart and copy it to the clipboard.
When we then hit paste the chart is black and white...why is it changing the
chart color to black and white when we set excelApp.Visible = false?

We use Microsoft.Office.Interop.Excel.dll  Version10.0.4504.0.

thanks
jahrens
Walter Wang [MSFT] - 08 Mar 2007 14:01 GMT
Hi jahrens,

I'm not able to reproduce the issue on my side using Office 11 and
following VB.NET 2005 code:

1) I first created an excel file which contains a simple chart (colorful)
2) Using following code, I created an instance of Excel (hidden) and opened
the file, then locate the chart and copy to clipboard, then paste into
paint brush and it's fine:

       Dim app As New Excel.Application
       Dim wkbk As Excel.Workbook = app.Workbooks.Open("book1.xlsx")
       Dim wkst As Excel.Worksheet = wkbk.Worksheets(1)
       Dim chart1 As Excel.ChartObject = wkst.ChartObjects(1)
       chart1.CopyPicture(Excel.XlPictureAppearance.xlScreen,
Excel.XlCopyPictureFormat.xlBitmap)

I understand that you're creating the chart on the fly. Could you please
post your code here so that I can learn how you created the chart? Thanks.

Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Jörgen Ahrens - 08 Mar 2007 15:40 GMT
Hi Walter

Yes we are creating the code on the fly and it has to work for Office
XP/2003.
On a pc with Office 2007 it works perfect. we cant see what we are doing
wrong for office xp and 2003...

Thanks for looking into it!

jahrens

Here is our code (C#):

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Reflection;

namespace WindowsApplication2
{
   public partial class Form1 : Form
   {
       private ApplicationClass excelApp;
       private Workbook wBook;
       private Worksheet wSheetPDC;
       private System.Globalization.CultureInfo originalCulture;
       private System.Globalization.CultureInfo englishCulture = new
System.Globalization.CultureInfo("en-US");

       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           originalCulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
           System.Threading.Thread.CurrentThread.CurrentCulture =
englishCulture;
           excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
           excelApp.IgnoreRemoteRequests = true;
           excelApp.WindowState = XlWindowState.xlMinimized;
           excelApp.Visible = false;
           excelApp.UserControl = false;

           wBook = excelApp.Workbooks.Add(System.Reflection.Missing.Value);
           wSheetPDC = (Worksheet)wBook.Sheets[1];

           //Get ranges
           Range range = wSheetPDC.get_Range(wSheetPDC.Cells[1, 1],
wSheetPDC.Cells[100, 3]);

           //Get value objects
           Object[,] values = (Object[,])range.Value2;

           Random r = new Random((int)DateTime.Now.Ticks);

           for (int i = 0; i < 100; i++)
           {

               values[i + 1, 1] = i;
               values[i + 1, 2] = r.Next();
               values[i + 1, 3] = r.Next();
           }

           range.Value2 = values;

           //Create chart
           ChartObjects charts =
(ChartObjects)wSheetPDC.ChartObjects(Type.Missing);
           ChartObject chartObj = charts.Add(100, 100, 700, 500);
           Chart chart = chartObj.Chart;
           chart.ChartArea.AutoScaleFont = false;

           //Get series 1
           Series series =
((SeriesCollection)chart.SeriesCollection(Missing.Value)).NewSeries();

           //Set chart type
           series.ChartType = XlChartType.xlXYScatterLines;

           //Set Values
           series.XValues = range.get_Range(range[1, 1], range[100, 1]);
           series.Values = range.get_Range(range[1, 2], range[100, 2]);
           series.Name = "Some data";
           series.Border.ColorIndex = 45;
           series.Border.Weight = XlBorderWeight.xlThin;
           series.Border.LineStyle = XlLineStyle.xlContinuous;
           if (series.Border.Color is double)
           {
               series.MarkerBackgroundColor =
(int)(double)series.Border.Color;
               series.MarkerForegroundColor =
(int)(double)series.Border.Color;
           }
           else
           {
               series.MarkerBackgroundColor = (int)series.Border.Color;
               series.MarkerForegroundColor = (int)series.Border.Color;
           }
           series.MarkerStyle = XlMarkerStyle.xlMarkerStyleDiamond;

           //Get series 2
           series =
((SeriesCollection)chart.SeriesCollection(Missing.Value)).NewSeries();

           //Set chart type
           series.ChartType = XlChartType.xlXYScatterLines;

           //Set Values
           series.XValues = range.get_Range(range[1, 1], range[100, 1]);
           series.Values = range.get_Range(range[1, 3], range[100, 3]);
           series.Name = "Some data";
           series.Border.ColorIndex = 23;
           series.Border.Weight = XlBorderWeight.xlThin;
           series.Border.LineStyle = XlLineStyle.xlContinuous;
           if (series.Border.Color is double)
           {
               series.MarkerBackgroundColor =
(int)(double)series.Border.Color;
               series.MarkerForegroundColor =
(int)(double)series.Border.Color;
           }
           else
           {
               series.MarkerBackgroundColor = (int)series.Border.Color;
               series.MarkerForegroundColor = (int)series.Border.Color;
           }
           series.MarkerStyle = XlMarkerStyle.xlMarkerStyleDiamond;

           //Set Title
           chart.HasTitle = true;
           chart.ChartTitle.Font.Size = 10.0;
           chart.ChartTitle.Text = "Chart Title";

           //Set Legend to false
           chart.HasLegend = false;

           //Set Axis and other stuff

           //Set xAxis
           Axis xAxis = (Axis)chart.Axes(XlAxisType.xlCategory,
XlAxisGroup.xlPrimary);
           xAxis.HasTitle = true;
           xAxis.AxisTitle.Text = "Some x Axis";
           xAxis.AxisTitle.Font.Size = 8;
           xAxis.TickLabels.Font.Size = 8;
           xAxis.MaximumScale = 100;
           xAxis.MinimumScale = 0.0;
           xAxis.HasMajorGridlines = true;

           //Set yAxis
           Axis yAxis = (Axis)chart.Axes(XlAxisType.xlValue,
XlAxisGroup.xlPrimary);
           yAxis.HasTitle = true;
           yAxis.AxisTitle.Text = "Some y Axis";
           yAxis.AxisTitle.Font.Size = 9;
           yAxis.TickLabels.Font.Size = 8;

           chart.CopyPicture(XlPictureAppearance.xlScreen,
XlCopyPictureFormat.xlBitmap, XlPictureAppearance.xlScreen);

           ReleaseComObject(series);
           series = null;

           ReleaseComObject(xAxis);
           xAxis = null;

           ReleaseComObject(yAxis);
           yAxis = null;

           ReleaseComObject(charts);
           charts = null;

           ReleaseComObject(chartObj);
           chartObj = null;

           ReleaseComObject(wSheetPDC);
           wSheetPDC = null;

           wBook.Close(false, Missing.Value, Missing.Value);
           ReleaseComObject(wBook);
           wBook = null;

           excelApp.IgnoreRemoteRequests = false;
           excelApp.UserControl = true;
           excelApp.Quit();
           ReleaseComObject(excelApp);
           excelApp = null;

           System.Threading.Thread.CurrentThread.CurrentCulture =
originalCulture;
       }

       private void ReleaseComObject(Object o)
       {
           if (o == null)
               return;

           try
           {
               while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(o) != 0) ;
           }
           catch (Exception ex)
           {
               System.Diagnostics.Debug.WriteLine(ex.ToString());
           }
       }
   }
}
Walter Wang [MSFT] - 09 Mar 2007 10:13 GMT
Hi jahrens,

I've tried your code on Windows XP SP2, Visual Studio 2005, Office 2003 and
Office 2003 PIAs; made the excel Visible by "excelApp.Visible = true".
After clicking the button, I pasted the picture in paint brush and I still
saw a picture in black/white.

Since I'm not familiar with Excel, I will try to incorporate your code in
Excel VBA to see if it's correctly generating the colorful chart.

I'll get back to you later when I got further result. Thanks for your
patience and understanding.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 14 Mar 2007 14:23 GMT
Hi jahrens,

I'm consulting this question within our internal discussion list and got
some updated information so far: it seems using chart.SetSourceData() then
get the chart.SeriesCollection(1) as the series will have the color
correctly applied. I'm still researching on how to get multiple series on
the chart if we use SetSourceData(). I'll keep you posted when I get
further information. Thanks for your patience and understanding.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 14 Mar 2007 14:41 GMT
Hi jahrens,

Please try following code and let me know the result:

       private void button1_Click(object sender, EventArgs e)
       {
           originalCulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
           System.Threading.Thread.CurrentThread.CurrentCulture =
englishCulture;
           excelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
           excelApp.IgnoreRemoteRequests = true;
           excelApp.WindowState = XlWindowState.xlMinimized;
           excelApp.Visible = false;
           excelApp.UserControl = false;

           wBook = excelApp.Workbooks.Add(System.Reflection.Missing.Value);
           wSheetPDC = (Worksheet)wBook.Sheets[1];

           //Get ranges
           Range range = wSheetPDC.get_Range(wSheetPDC.Cells[1, 1],
wSheetPDC.Cells[100, 3]);

           //Get value objects
           Object[,] values = (Object[,])range.Value2;

           Random r = new Random((int)DateTime.Now.Ticks);

           for (int i = 0; i < 100; i++)
           {

               values[i + 1, 1] = i;
               values[i + 1, 2] = r.Next();
               values[i + 1, 3] = r.Next();
           }

           range.Value2 = values;

           //Create chart
           ChartObjects charts =
(ChartObjects)wSheetPDC.ChartObjects(Type.Missing);
           ChartObject chartObj = charts.Add(100, 100, 700, 500);
           Chart chart = chartObj.Chart;
           chart.ChartArea.AutoScaleFont = false;

           chart.ChartType = XlChartType.xlXYScatterLines;
           chart.SetSourceData(range, Missing.Value);

           //Get series 1
           // Series series =
((SeriesCollection)chart.SeriesCollection(Missing.Value)).NewSeries();
           Series series = (Series)chart.SeriesCollection(1);

           //Set chart type
           // series.ChartType = XlChartType.xlXYScatterLines;

           //Set Values
           // series.XValues = range.get_Range(range[1, 1], range[100, 1]);
           // series.Values = range.get_Range(range[1, 2], range[100, 2]);
           series.Name = "Some data";
           series.Border.ColorIndex = 45;
           
           series.Border.Weight = XlBorderWeight.xlThin;
           series.Border.LineStyle = XlLineStyle.xlContinuous;
           if (series.Border.Color is double)
           {
               series.MarkerBackgroundColor =
(int)(double)series.Border.Color;
               series.MarkerForegroundColor =
(int)(double)series.Border.Color;
           }
           else
           {
               series.MarkerBackgroundColor = (int)series.Border.Color;
               series.MarkerForegroundColor = (int)series.Border.Color;
           }
           series.MarkerStyle = XlMarkerStyle.xlMarkerStyleDiamond;

           //Get series 2
           // series =
((SeriesCollection)chart.SeriesCollection(Missing.Value)).NewSeries();
           series = (Series)chart.SeriesCollection(2);

           //Set chart type
           //series.ChartType = XlChartType.xlXYScatterLines;

           //Set Values
           //series.XValues = range.get_Range(range[1, 1], range[100, 1]);
           // series.Values = range.get_Range(range[1, 3], range[100, 3]);
           series.Name = "Some data";
           series.Border.ColorIndex = 23;
           series.Border.Weight = XlBorderWeight.xlThin;
           series.Border.LineStyle = XlLineStyle.xlContinuous;
           if (series.Border.Color is double)
           {
               series.MarkerBackgroundColor =
(int)(double)series.Border.Color;
               series.MarkerForegroundColor =
(int)(double)series.Border.Color;
           }
           else
           {
               series.MarkerBackgroundColor = (int)series.Border.Color;
               series.MarkerForegroundColor = (int)series.Border.Color;
           }
           series.MarkerStyle = XlMarkerStyle.xlMarkerStyleDiamond;

           //Set Title
           chart.HasTitle = true;
           chart.ChartTitle.Font.Size = 10.0;
           chart.ChartTitle.Text = "Chart Title";

           //Set Legend to false
           chart.HasLegend = true;

           //Set Axis and other stuff

           //Set xAxis
           Axis xAxis = (Axis)chart.Axes(XlAxisType.xlCategory,
XlAxisGroup.xlPrimary);
           xAxis.HasTitle = true;
           xAxis.AxisTitle.Text = "Some x Axis";
           xAxis.AxisTitle.Font.Size = 8;
           xAxis.TickLabels.Font.Size = 8;
           xAxis.MaximumScale = 100;
           xAxis.MinimumScale = 0.0;
           xAxis.HasMajorGridlines = true;

           //Set yAxis
           Axis yAxis = (Axis)chart.Axes(XlAxisType.xlValue,
XlAxisGroup.xlPrimary);
           yAxis.HasTitle = true;
           yAxis.AxisTitle.Text = "Some y Axis";
           yAxis.AxisTitle.Font.Size = 9;
           yAxis.TickLabels.Font.Size = 8;

           chart.CopyPicture(XlPictureAppearance.xlScreen,
XlCopyPictureFormat.xlBitmap, XlPictureAppearance.xlScreen);

           ReleaseComObject(series);
           series = null;

           ReleaseComObject(xAxis);
           xAxis = null;

           ReleaseComObject(yAxis);
           yAxis = null;

           ReleaseComObject(charts);
           charts = null;

           ReleaseComObject(chartObj);
           chartObj = null;

           ReleaseComObject(wSheetPDC);
           wSheetPDC = null;

           wBook.Close(false, Missing.Value, Missing.Value);
           ReleaseComObject(wBook);
           wBook = null;

           excelApp.IgnoreRemoteRequests = false;
           excelApp.UserControl = true;
           excelApp.Quit();
           ReleaseComObject(excelApp);
           excelApp = null;

           System.Threading.Thread.CurrentThread.CurrentCulture =
originalCulture;
       }

       private void ReleaseComObject(Object o)
       {
           if (o == null)
               return;

           try
           {
               while
(System.Runtime.InteropServices.Marshal.ReleaseComObject(o) != 0) ;
           }
           catch (Exception ex)
           {
               System.Diagnostics.Debug.WriteLine(ex.ToString());
           }
       }

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Jörgen Ahrens - 15 Mar 2007 14:49 GMT
Thanks for your help! its great! it works!

jahrens

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.