0

I have a user control library that will use as a custom tooltip for another window, the user control holds ImageSource of a imagebrush that reference to a directory . Inside the window, I have another class that will generate an image file to the directory same as the imagebrush.

However i get an error below after run. The call stack contained only external code. 'The invocation of the constructor on type 'WpfApplication2.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'

Here is my snippet.

UserControl1.xaml

  <UserControl.Resources>
    <Style TargetType="Rectangle">
        <Setter Property="Fill">
            <Setter.Value>
                <ImageBrush  ImageSource="C:\Users\user\Desktop\wpf\WpfApplication2\WpfApplication2\Images/QR.png" Stretch="Fill" />
            </Setter.Value>
        </Setter>
    </Style> 
</UserControl.Resources>

DisplayWindow.xaml

 xmlns:myToolTip="clr-namespace:WpfControlLibrary2;assembly=WpfControlLibrary2"

 <myToolTip:UserControl1 Visibility="Collapsed" x:Name="customToolTip" Width="468" Height="700" />

QRCodeEncoder.cs

   public QrCodeEncodercs(string encodeString)
    {
        QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
        QrCode qrCode = new QrCode();
        //const string encodeString = "123";
        qrEncoder.TryEncode(encodeString, out qrCode);

        Renderer renderer = new Renderer(11, System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
        renderer.CreateImageFile(qrCode.Matrix, @"C:\Users\user\Desktop\wpf\WpfApplication2\WpfApplication2\Images\QR.png\Images\QR.png",
            ImageFormat.Png);

    }

DisplayWindow.xaml.cs

When i run the constructor of the class above. The error mentioned above happened.

qce = new QrCodeEncodercs(videoName);

What happened actually? Any guidance? Thanks in advance.

Edit____InnerException

 InnerException: System.Runtime.InteropServices.ExternalException
   HResult=-2147467259
   Message=A generic error occurred in GDI+.
   Source=System.Drawing
   ErrorCode=-2147467259
   StackTrace:
        at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
        at Gma.QrCodeNet.Encoding.Windows.Controls.Renderer.CreateImageFile(BitMatrix matrix, String fileName, ImageFormat imageFormat)
        at WpfApplication2.DisplayWindow.QrCodeEncodercs(String encodeString) in c:\Users\user\Desktop\wpf\KinectREAL\WpfApplication2\WpfApplication2\DisplayWindow..xaml.cs:line 127
        at WpfApplication2.DisplayWindow.PopulateVideoListAndFirstVideo() in c:\Users\user\Desktop\wpf\KinectREAL\WpfApplication2\WpfApplication2\DisplayWindow..xaml.cs:line 157
        at WpfApplication2.DisplayWindow..ctor() in c:\Users\user\Desktop\wpf\KinectREAL\WpfApplication2\WpfApplication2\DisplayWindow..xaml.cs:line 79
        at WpfApplication2.MainWindow..ctor() in c:\Users\user\Desktop\wpf\KinectREAL\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:line 45
   InnerException: 
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user1884304
  • 39
  • 1
  • 8
  • Hi..Thanks for reply. I updated my innerexception already. Can you check for me? I am confuse. Thanks – user1884304 Dec 31 '12 at 09:21
  • Check if you have write permission to the folder you are saving imge(http://weblogs.asp.net/anasghanem/archive/2009/02/28/solving-quot-a-generic-error-occurred-in-gdi-quot-exception.aspx) also take a look to this discussion(http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream) – Arsen Mkrtchyan Dec 31 '12 at 10:50
  • I think I have the write permission as I success to save file with other name but not with the same name. I have this error "The process cannot access the file 'C:\...\QR.png' because it is being used by another process." – user1884304 Dec 31 '12 at 11:38
  • ok, than your or another process have open the same file in the other places. check if you close correctly file everywhere you open it – Arsen Mkrtchyan Dec 31 '12 at 15:34
  • thanks for your link provided, its basically guide me to the correct way. Thanks again. – user1884304 Jan 01 '13 at 15:19

2 Answers2

0

These Generic error in GDI+ exceptions can be cryptic at times. One cause I've experienced in the past is when an image file has an incorrect extension (eg: a JPG ssaved with a GIF extension). My second guess would be that the filename in the path of the file you're saving to is invalid. Does the directory exist?

If the file is in use, you could copy it to a temporary directory with File.Copy and get a temporary filename with Path.GetTempFilename().

Echilon
  • 10,064
  • 33
  • 131
  • 217
  • Yes, the path existed. I can save by using another filename. My guess is the imagesource of imagebrush holding and opening the path, so I can't rewrite the file. Any idea how I can correct my problem? Thanks for reply anyway. – user1884304 Dec 31 '12 at 10:06
0

I manage to solve after some trying and it works so far. Thanks again.

  System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
        bi.BeginInit();
        bi.UriSource = new   Uri(@"C:\Users\user\Desktop\wpf\WpfApplication2\WpfApplication2\Images\QR.png", UriKind.RelativeOrAbsolute);
        bi.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
        bi.EndInit();

        image.Source = bi;
user1884304
  • 39
  • 1
  • 8