I have a Silverlight 3 app that allows the user to upload images, and then is supposed to display them. The image gets uploaded fine (I can view it in the browser), but I get the following error from Silverlight:
Message: Sys.InvalidOperationException: ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR
Line: 453
Char: 17
Code: 0
URI: http://www.greektools.net/ScriptResource.axd?d=J4B4crBmh4Qxr3eVyHw3QRgAKpCaa6XLu8H_2zpAs7eWeADXG9jQu_NCTxorhOs1x6sWoSKHEqAL37LcpWepfg2&t=fffffffffd030d68
Fiddler reports: There is a problem with the resource you are looking for, and it cannot be displayed.
Thanks in advance for your help.
Here it is in three files. This is also available at my blog, which is listed in my profile.
XAML (imageuploader.xaml):
<UserControl x:Class="ImageEditor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" MouseMove="UserControl_MouseMove">
<Grid x:Name="LayoutRoot" Background="Azure" Width="800" Height="600">
<Image x:Name="MainImage" Source="images/image.jpg" Width="300" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<Button x:Name="UploadImage" Click="UploadImage_Click" Content="Upload"></Button>
</Grid>
</UserControl>
Code (imageuploader.xaml.cs):
private void UploadImage_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
UploadFile(dlg.File.Name, dlg.File.OpenRead());
StatusText.Text = dlg.File.Name;
}
else
{
StatusText.Text = "No file selected...";
}
}
private void UploadFile(string fileName, Stream data)
{
string host = Application.Current.Host.Source.AbsoluteUri;
host = host.Remove(host.IndexOf("/ClientBin"));
UriBuilder ub = new UriBuilder(host + "/receiver.ashx");
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.WriteStreamClosed += (sender, e) =>
{
LoadImage(fileName);
};
c.OpenWriteAsync(ub.Uri);
}
private void LoadImage(string fileName)
{
//
// Creating WebClient object and setting up events
//
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
//downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
//
// Specify Image to load
//
string host = Application.Current.Host.Source.AbsoluteUri;
host = host.Remove(host.IndexOf("/ClientBin"));
fileName = host + "/images/" + fileName;
downloader.OpenReadAsync(new Uri(fileName, UriKind.Absolute));
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//
// Create a new BitmapImage and load the stream
//
BitmapImage loadedImage = new BitmapImage();
loadedImage.SetSource(e.Result);
//
// Setting our BitmapImage as the source of a imageControl control I have in XAML
//
MainImage.Source = loadedImage;
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
Handler (receiver.ashx):
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class receiver : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString["filename"].ToString();
using (FileStream fs = File.Create(context.Server.MapPath("~/images/" + filename)))
{
SaveFile(context.Request.InputStream, fs);
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Related
I have code to import Images from Clipboard to RichtTextBox.
Image i = new Image();
i.Source = Clipboard.GetImage();
paragraph.Inlines.Add(i);
When I try to delete image and press Undo() i have exception.
No matching constructor found on type
'System.Windows.Interop.InteropBitmap'. You can use the Arguments or
FactoryMethod directives to construct this type.' Line number '1' and
line position '226'.
This is because XAML generated by RichTextBox look like below:
<Image.Source><swi:InteropBitmap /></Image.Source>
I try to change type of BitmapSource to BitmapImage. But in this situation i have XAML:
<Image.Source><BitmapImage BaseUri="{x:Null}" /></Image.Source></Image>
And after delete,Undo i have exception:
Exception thrown: 'System.Windows.Markup.XamlParseException' in
PresentationFramework.dll
Additional information: 'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.' Line
number '1' and line position '243'.
I even try the InlineImage from: http://wpftutorial.net/InlineImagesXaml.html
<InlineImage Width="100" Height="100" Stretch="Fill">
<![CDATA[iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAB3RJTUUH2AQP
SFlzAAALEgAACxIB0t1+/AAAAARnQU1BAACxjwv8YQUAAAnOSURBVHjaxVcLcBvVFT1vV
ki3Hju3GCQnGjkObONQkJkxCSIHQQGnIdEr5TFs+LaGl7RRCSUvDp8nglH4mDGQ6EwZIm=]]>
</InlineImage>
Even in this situation i have exception in Undo/Redo operation. Is there any possibility without writing own Undo/Redo operation to handle this situation.
I am confused weather to edit old answer or add new one. So I am going for a new one.
Using below approach I can now copy an existing image file and paste it in RTB, and also I can now copy some unsaved image data from MSPaint, Photoshop and paste that too. After pressing Save button, rtf file is saved and opens nicely in MSWord as expected.
Ctrl+Z is not working, as image data is in stream. I am working on it. Ctrl+Z is not a problem when image is copied as file.
Any more doubts are most welcome. Code below is complete can be used as is.
ImageCode.cs for getting image stored in clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Runtime.InteropServices;
namespace WpfRichTextBox._32648134
{
public class ImageCode
{
public static ImageSource ImageFromClipboardDibAsSource()
{
MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;
if (ms != null)
{
byte[] dibBuffer = new byte[ms.Length];
ms.Read(dibBuffer, 0, dibBuffer.Length);
BITMAPINFOHEADER infoHeader =
BinaryStructConverter.FromByteArray<BITMAPINFOHEADER>(dibBuffer);
int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
int infoHeaderSize = infoHeader.biSize;
int fileSize = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;
BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER();
fileHeader.bfType = BITMAPFILEHEADER.BM;
fileHeader.bfSize = fileSize;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;
byte[] fileHeaderBytes =
BinaryStructConverter.ToByteArray<BITMAPFILEHEADER>(fileHeader);
MemoryStream msBitmap = new MemoryStream();
msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
msBitmap.Seek(0, SeekOrigin.Begin);
BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnDemand;
img.CreateOptions = BitmapCreateOptions.DelayCreation;
img.StreamSource = msBitmap;
img.EndInit();
return img;
}
return null;
}
public static MemoryStream ImageFromClipboardDibAsStream()
{
MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;
if (ms != null)
{
byte[] dibBuffer = new byte[ms.Length];
ms.Read(dibBuffer, 0, dibBuffer.Length);
BITMAPINFOHEADER infoHeader =
BinaryStructConverter.FromByteArray<BITMAPINFOHEADER>(dibBuffer);
int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
int infoHeaderSize = infoHeader.biSize;
int fileSize = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;
BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER();
fileHeader.bfType = BITMAPFILEHEADER.BM;
fileHeader.bfSize = fileSize;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;
byte[] fileHeaderBytes =
BinaryStructConverter.ToByteArray<BITMAPFILEHEADER>(fileHeader);
MemoryStream msBitmap = new MemoryStream();
msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
msBitmap.Seek(0, SeekOrigin.Begin);
return msBitmap;
}
return null;
}
}
public static class BinaryStructConverter
{
public static T FromByteArray<T>(byte[] bytes) where T : struct
{
IntPtr ptr = IntPtr.Zero;
try
{
int size = Marshal.SizeOf(typeof(T));
ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(bytes, 0, ptr, size);
object obj = Marshal.PtrToStructure(ptr, typeof(T));
return (T)obj;
}
finally
{
if (ptr != IntPtr.Zero)
Marshal.FreeHGlobal(ptr);
}
}
public static byte[] ToByteArray<T>(T obj) where T : struct
{
IntPtr ptr = IntPtr.Zero;
try
{
int size = Marshal.SizeOf(typeof(T));
ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(obj, ptr, true);
byte[] bytes = new byte[size];
Marshal.Copy(ptr, bytes, 0, size);
return bytes;
}
finally
{
if (ptr != IntPtr.Zero)
Marshal.FreeHGlobal(ptr);
}
}
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
struct BITMAPFILEHEADER
{
public static readonly short BM = 0x4d42; // BM
public short bfType;
public int bfSize;
public short bfReserved1;
public short bfReserved2;
public int bfOffBits;
}
[StructLayout(LayoutKind.Sequential)]
struct BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
public short biPlanes;
public short biBitCount;
public int biCompression;
public int biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public int biClrUsed;
public int biClrImportant;
}
}
MainWindow.xaml
<Window x:Class="WpfRichTextBox._32648134.Win32648134"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Win32648134" Height="600" Width="700">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="41*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="21*"/>
</Grid.RowDefinitions>
<RichTextBox x:Name="RtbCompose" Width="500" Height="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<FlowDocument x:Name="FdDocument">
<Paragraph x:Name="Para1"></Paragraph>
</FlowDocument>
</RichTextBox>
<Button x:Name="BtnCopyImgFile" Content="Paste image" HorizontalAlignment="Left" Margin="96,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="BtnCopyImgFile_Click"/>
<Button x:Name="BtnSave" Content="Save" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Top" Width="75" Margin="521,10,0,0" Click="BtnSave_Click"/>
<Button x:Name="BtnCopyImgData" Content="Paste image data" HorizontalAlignment="Left" Margin="190,11,0,0" Grid.Row="1" VerticalAlignment="Top" Click="BtnCopyImgData_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
namespace WpfRichTextBox._32648134
{
/// <summary>
/// Interaction logic for Win32648134.xaml
/// </summary>
public partial class Win32648134 : Window
{
public Win32648134()
{
InitializeComponent();
}
private void BtnCopyImgFile_Click(object sender, RoutedEventArgs e)
{
Image i = new Image();
if (Clipboard.ContainsFileDropList())
{
StringCollection fileNames = Clipboard.GetFileDropList();
BitmapImage img = new BitmapImage(new Uri(fileNames[0], UriKind.Absolute));
i.Source = img;
Para1.Inlines.Add(i);
}
Para1.Inlines.Add(new Run("first rtb app"));
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
TextRange allText = new TextRange(RtbCompose.Document.ContentStart, RtbCompose.Document.ContentEnd);
FileStream stream = new FileStream(#"I:\RTB.rtf", FileMode.Create);
allText.Save(stream, DataFormats.Rtf);
if (stream != null)
stream.Close();
}
private void BtnCopyImgData_Click(object sender, RoutedEventArgs e)
{
bool hasImgData = Clipboard.ContainsImage();
Image i = new Image();
if (hasImgData)
{
BitmapSource imgData = (BitmapSource)ImageCode.ImageFromClipboardDibAsSource();
i.Source = imgData;
Para1.Inlines.Add(i);
}
Para1.Inlines.Add(new Run("rtb app, image comes from image data instead of file"));
}
}
}
I found the solution. I had to copy class WpfLoad from RichTextBox source code. This code save Package as Stream with Image as Content and Uri as Source of the document.
Because class WpfPayload is internal I don't have access to this class. I need to create my own.
Below is the source of the class WpfPayLoad.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
namespace YATE
{
internal class WpfPayload
{
private const string XamlPayloadDirectory = "/Xaml"; //
private const string XamlEntryName = "/Document.xaml"; //
private const string XamlContentType = "application/vnd.ms-wpf.xaml+xml";
private const string XamlImageName = "/Image"; //
private const string XamlRelationshipFromPackageToEntryPart = "http://schemas.microsoft.com/wpf/2005/10/xaml/entry";
private const string XamlRelationshipFromXamlPartToComponentPart = "http://schemas.microsoft.com/wpf/2005/10/xaml/component";
internal const string ImageBmpContentType = "image/bmp";
private const string ImageGifContentType = "image/gif";
private const string ImageJpegContentType = "image/jpeg";
private const string ImageTiffContentType = "image/tiff";
private const string ImagePngContentType = "image/png";
private const string ImageBmpFileExtension = ".bmp";
private const string ImageGifFileExtension = ".gif";
private const string ImageJpegFileExtension = ".jpeg";
private const string ImageJpgFileExtension = ".jpg";
private const string ImageTiffFileExtension = ".tiff";
private const string ImagePngFileExtension = ".png";
Package _package = null;
private static BitmapEncoder GetBitmapEncoder(string imageContentType)
{
BitmapEncoder bitmapEncoder;
switch (imageContentType)
{
case ImageBmpContentType:
bitmapEncoder = new BmpBitmapEncoder();
break;
case ImageGifContentType:
bitmapEncoder = new GifBitmapEncoder();
break;
case ImageJpegContentType:
bitmapEncoder = new JpegBitmapEncoder();
//
break;
case ImageTiffContentType:
bitmapEncoder = new TiffBitmapEncoder();
break;
case ImagePngContentType:
bitmapEncoder = new PngBitmapEncoder();
break;
default:
bitmapEncoder = null;
break;
}
return bitmapEncoder;
}
// Returns a file extension corresponding to a given imageContentType
private static string GetImageFileExtension(string imageContentType)
{
string imageFileExtension;
switch (imageContentType)
{
case ImageBmpContentType:
imageFileExtension = ImageBmpFileExtension;
break;
case ImageGifContentType:
imageFileExtension = ImageGifFileExtension;
break;
case ImageJpegContentType:
imageFileExtension = ImageJpegFileExtension;
break;
case ImageTiffContentType:
imageFileExtension = ImageTiffFileExtension;
break;
case ImagePngContentType:
imageFileExtension = ImagePngFileExtension;
break;
default:
imageFileExtension = null;
break;
}
return imageFileExtension;
}
WpfPayload(Package p = null)
{
this._package = p;
}
private Package CreatePackage(Stream stream)
{
_package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
return _package;
}
// Generates a image part Uri for the given image index
private static string GetImageName(int imageIndex, string imageContentType)
{
string imageFileExtension = GetImageFileExtension(imageContentType);
return XamlImageName + (imageIndex + 1) + imageFileExtension;
}
// Generates a relative URL for using from within xaml Image tag.
private static string GetImageReference(string imageName)
{
return "." + imageName; // imageName is supposed to be created by GetImageName method
}
private PackagePart CreateWpfEntryPart()
{
// Define an entry part uri
Uri entryPartUri = new Uri(XamlPayloadDirectory + XamlEntryName, UriKind.Relative);
// Create the main xaml part
PackagePart part = _package.CreatePart(entryPartUri, XamlContentType, CompressionOption.Normal);
// Compression is turned off in this mode.
//NotCompressed = -1,
// Compression is optimized for a resonable compromise between size and performance.
//Normal = 0,
// Compression is optimized for size.
//Maximum = 1,
// Compression is optimized for performance.
//Fast = 2 ,
// Compression is optimized for super performance.
//SuperFast = 3,
// Create the relationship referring to the entry part
PackageRelationship entryRelationship = _package.CreateRelationship(entryPartUri, TargetMode.Internal, XamlRelationshipFromPackageToEntryPart);
return part;
}
private void CreateImagePart(PackagePart sourcePart, BitmapSource imageSource, string imageContentType, int imageIndex)
{
// Generate a new unique image part name
string imagePartUriString = GetImageName(imageIndex, imageContentType);
// Define an image part uri
Uri imagePartUri = new Uri(XamlPayloadDirectory + imagePartUriString, UriKind.Relative);
// Create a part for the image
PackagePart imagePart = _package.CreatePart(imagePartUri, imageContentType, CompressionOption.NotCompressed);
// Create the relationship referring from the enrty part to the image part
PackageRelationship componentRelationship = sourcePart.CreateRelationship(imagePartUri, TargetMode.Internal, XamlRelationshipFromXamlPartToComponentPart);
// Encode the image data
BitmapEncoder bitmapEncoder = GetBitmapEncoder(imageContentType);
bitmapEncoder.Frames.Add(BitmapFrame.Create(imageSource));
// Save encoded image data into the image part in the package
Stream imageStream = imagePart.GetStream();
using (imageStream)
{
bitmapEncoder.Save(imageStream);
}
}
internal PackagePart GetWpfEntryPart()
{
PackagePart wpfEntryPart = null;
// Find a relationship to entry part
PackageRelationshipCollection entryPartRelationships = _package.GetRelationshipsByType(XamlRelationshipFromPackageToEntryPart);
PackageRelationship entryPartRelationship = null;
foreach (PackageRelationship packageRelationship in entryPartRelationships)
{
entryPartRelationship = packageRelationship;
break;
}
// Get a part referred by this relationship
if (entryPartRelationship != null)
{
// Get entry part uri
Uri entryPartUri = entryPartRelationship.TargetUri;
// Get the enrty part
wpfEntryPart = _package.GetPart(entryPartUri);
}
return wpfEntryPart;
}
[System.Security.SecurityCritical]
internal static Stream SaveImage(BitmapSource bitmapSource, string imageContentType)
{
MemoryStream stream = new MemoryStream();
// Create the wpf package in the stream
WpfPayload wpfPayload = new WpfPayload();
using (wpfPayload.CreatePackage(stream))
{
PackagePart xamlEntryPart = wpfPayload.CreateWpfEntryPart();
Stream xamlPartStream = xamlEntryPart.GetStream();
using (xamlPartStream)
{
int imageIndex = 0;
string imageReference = GetImageReference(GetImageName(imageIndex, imageContentType));
StreamWriter xamlPartWriter = new StreamWriter(xamlPartStream);
using (xamlPartWriter)
{
string xamlText =
"<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
"<InlineUIContainer><Image " +
"Width=\"" +
bitmapSource.Width + "\" " +
"Height=\"" +
bitmapSource.Height + "\" " +
"><Image.Source><BitmapImage CacheOption=\"OnLoad\" UriSource=\"" +
imageReference +
"\"/></Image.Source></Image></InlineUIContainer></Span>";
xamlPartWriter.Write(xamlText);
}
wpfPayload.CreateImagePart(xamlEntryPart, bitmapSource, imageContentType, imageIndex);
}
}
return stream;
}
static int _wpfPayloadCount; // used to disambiguate between all acts of loading from different WPF payloads.
internal static object LoadElement(Stream stream)
{
Package package = Package.Open(stream, FileMode.Open, FileAccess.Read);
WpfPayload wpfPayload = new WpfPayload(package);
PackagePart xamlEntryPart = wpfPayload.GetWpfEntryPart();
int newWpfPayoutCount = _wpfPayloadCount++;
Uri payloadUri = new Uri("payload://wpf" + newWpfPayoutCount, UriKind.Absolute);
Uri entryPartUri = PackUriHelper.Create(payloadUri, xamlEntryPart.Uri); // gives an absolute uri of the entry part
Uri packageUri = PackUriHelper.GetPackageUri(entryPartUri); // extracts package uri from combined package+part uri
PackageStore.AddPackage(packageUri, wpfPayload.Package); // Register the package
ParserContext parserContext = new ParserContext();
parserContext.BaseUri = entryPartUri;
object xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext);
// Remove the temporary uri from the PackageStore
PackageStore.RemovePackage(packageUri);
return xamlObject;
}
public Package Package
{
get { return _package; }
}
};
}
Next if we have below RichTextBox:
<RichTextBox x:Name="RtbCompose" Width="500" Height="300">
<FlowDocument x:Name="FdDocument">
<Paragraph x:Name="Para1"></Paragraph>
</FlowDocument>
</RichTextBox>
<Button Content="Paste image" HorizontalAlignment="Left" Margin="96,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
On Button_Click we can save our image from the ClipBoard. First I got the Stream of package and next we can convert this as XamlReader.Load().
private void Button_Click(object sender, RoutedEventArgs e)
{
BitmapSource image = Clipboard.GetImage();
Stream packagedImage = WpfPayload.SaveImage(image, WpfPayload.ImageBmpContentType);
object element = WpfPayload.LoadElement(packagedImage);
Para1.Inlines.Add(element as Span);
}
Result we can save with XAMLPackage.
public byte[] SaveAllContent(RichTextBox rtb)
{
var content = new TextRange(rtb.Document.ContentStart, rtb_Main.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
content.Save(ms, DataFormats.XamlPackage, true);
return ms.ToArray();
}
}
public void LoadAllContent(byte [] bd, RichTextBox rtb)
{
var content = new TextRange(rtb.Document.ContentStart, rtb_Main.Document.ContentEnd);
MemoryStream ms = new MemoryStream(bd);
content.Load(ms, System.Windows.DataFormats.XamlPackage);
}
With this solution Undo() and Redo() works fine :)
I'm currently converting someone else's code. It is written in C# Windows FORM. and I want it to be in C# WPF.
this is the original code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace FileReceiver
{
public partial class MainForm : Form
{
const int PORT = 1723;
public MainForm()
{
InitializeComponent();
}
private void resetControls()
{
progressBar1.Style = ProgressBarStyle.Marquee;
textBox1.Text = "Waiting for connection...";
}
protected override async void OnShown(EventArgs e)
{
// Listen
TcpListener listener = TcpListener.Create(PORT);
listener.Start();
textBox1.Text = "Waiting for connection...";
TcpClient client = await listener.AcceptTcpClientAsync();
NetworkStream ns = client.GetStream();
// Get file info
long fileLength;
string fileName;
{
byte[] fileNameBytes;
byte[] fileNameLengthBytes = new byte[4]; //int32
byte[] fileLengthBytes = new byte[8]; //int64
await ns.ReadAsync(fileLengthBytes, 0, 8); // int64
await ns.ReadAsync(fileNameLengthBytes, 0, 4); // int32
fileNameBytes = new byte[BitConverter.ToInt32(fileNameLengthBytes, 0)];
await ns.ReadAsync(fileNameBytes, 0, fileNameBytes.Length);
fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
fileName = ASCIIEncoding.ASCII.GetString(fileNameBytes);
}
// Get permission
if (MessageBox.Show(String.Format("Requesting permission to receive file:\r\n\r\n{0}\r\n{1} bytes long", fileName, fileLength), "", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
// Set save location
SaveFileDialog sfd = new SaveFileDialog();
sfd.CreatePrompt = false;
sfd.OverwritePrompt = true;
sfd.FileName = fileName;
if (sfd.ShowDialog() != DialogResult.OK)
{
ns.WriteByte(0); // Permission denied
return;
}
ns.WriteByte(1); // Permission grantedd
FileStream fileStream = File.Open(sfd.FileName, FileMode.Create);
// Receive
textBox1.Text = "Receiving...";
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Value = 0;
int read;
int totalRead = 0;
byte[] buffer = new byte[32 * 1024]; // 32k chunks
while ((read = await ns.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, read);
totalRead += read;
progressBar1.Value = (int)((100d * totalRead) / fileLength);
}
fileStream.Dispose();
client.Close();
MessageBox.Show("File successfully received");
resetControls();
}
}
}
I've successfully converted all the things in this code into WPF.
and the ONLY problem I am getting in WPF is this:
protected override async void OnShown(EventArgs e)
Error 8 'EmployeeLocatorv2.receiver.OnShown(System.EventArgs)': no suitable method found to override
This means that the class your main form is inheriting (probably Window or UserControl) does not have a virtual method named "OnShown" that you can override.
According to Chango V., Window.ContentRendered is the event you're looking for.
You can use the Window.ContentRendered event to know when the WPF form is rendered.
You can declare the event in your XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered_1">
And the corresponding code:
private void Window_ContentRendered_1(object sender, EventArgs e)
{
// Place your code here.
}
In WPF, ContentRendered has a similar behavior.
bool _hasShown;
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
if (!_hasShown)
{
_hasShown = true;
// void OnShown() code here!
}
}
I followed the examples here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662940%28v=vs.105%29.aspx
I made the live preview and stuff work, but how can I take a picture? This is what i tried:
private PhotoCaptureDevice _photoCaptureDevice = null;
string strImageName = "IG_Temp";
private NokiaImagingSDKEffects _cameraEffect = null;
private MediaElement _mediaElement = null;
private CameraStreamSource _cameraStreamSource = null;
private Semaphore _cameraSemaphore = new Semaphore(1, 1);
MediaLibrary library = new MediaLibrary();
private async void ShutterButton_Click(object sender, RoutedEventArgs e)
{
if (_cameraSemaphore.WaitOne(100))
{
await _photoCaptureDevice.FocusAsync();
_cameraSemaphore.Release();
CameraCaptureSequence seq = _photoCaptureDevice.CreateCaptureSequence(1);
await seq.StartCaptureAsync();
MemoryStream captureStream1 = new MemoryStream();
// Assign the capture stream.
seq.Frames[0].CaptureStream = captureStream1.AsOutputStream();
try
{
// Set the position of the stream back to start
captureStream1.Seek(0, SeekOrigin.Begin);
// Save photo as JPEG to the local folder.
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(strImageName, FileMode.Create, FileAccess.Write))
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the image to the local folder.
while ((bytesRead = captureStream1.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
}
finally
{
// Close image stream
captureStream1.Close();
}
// Navigate to the next page
Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate(new Uri("/Edit.xaml?name=" + strImageName, UriKind.Relative));
});
}
}
But I will get an error:
System.InvalidOperationException
in line: await seq.StartCaptureAsync();
What am I doing wrong?
this question is a follow up from this one. silverlight 4 image preview from tooltip on datagrid
This is my new problem, I have been trying to get a tool tip to pop up with the preview of the document that is returned in search results of my silverlight app. I have linked the image and it comes up with the correct image, however it opens in a new or separate window instead of the tooltip itself. Here is my code behind..
private void PPTImageToolTip(object sender, RoutedEventArgs e)
{
string docname = ((FrameworkElement)sender).DataContext.ToString();
string baseUri = "http://localhost:58904/ShowDocument.aspx?DocumentName=" + docname + "-ppt" + "&type=jpg";
var hostingWindow = HtmlPage.Window;
hostingWindow.Navigate(new Uri(baseUri, UriKind.Absolute), "_parent");
}
This is set to go to my ShowDocument.aspx page which handles this operation..
else if (File.Exists(strFullFilePath) && sType == "jpg")
{
fileStream = new FileStream(strFullFilePath, FileMode.Open, FileAccess.Read);
buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, Convert.ToInt32(fileStream.Length));
try
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(buffer);
}
catch (Exception ex)
{ }
}
I realize that it transfers to another 'page' but I have not been able to get that image or that page to show up in the tool tip itself instead of populating the new window. Is this because of my HtmlPage.window code? Or because the ShowDocument.aspx page is already called and it cant call back? Is there a workable solution to get the image to populate inside the tool tip? Or is there a way to repsonse.redirect into the silverlight control that holds the tooltip?
If your aim is to display the image inside the tooltip (and not in the html window), the following will work:
First the web service (ashx)
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String fileName = #"c:\PathToMyFile\Myfile.jpg";
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, Convert.ToInt32(fileStream.Length));
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(buffer);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Then to call the service from the Silverlight client:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) =>
{
using (Stream stream = e.Result)
{
BitmapImage img = new BitmapImage();
img.SetSource(stream);
// Update MyImage.Source. Use the Dispatcher to ensure this happens on the UI Thread
Dispatcher.BeginInvoke(() =>
{
MyImage.Source = img;
});
}
};
client.OpenReadAsync(new Uri(String.Format(BaseURL + "MyHandler.ashx")));
}
}
And finally the xaml for the view:
<Border x:Name="MyBorder" Width="100" Height="100" Background="Black">
<ToolTipService.ToolTip>
<Image x:Name="MyImage" />
</ToolTipService.ToolTip>
</Border>
I need to update a file which is on a remote server, using Silverlight and C#.
I created the file on the current machine and after that I tried to upload it using this example. It returned no error, but it doesn't upload my file either.
Could you help me?
this is the ashx code
<%# WebHandler Language="C#" Class="receiver" %>
using System;
using System.Web;
using System.IO;
public class receiver : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string filename = context.Request.QueryString["DB.xml"].ToString();
using (FileStream fs = File.Create(context.Server.MapPath("~/CLientBin" + filename)))
{
SaveFile(context.Request.InputStream, fs);
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
public bool IsReusable {
get {
return false;
}
}
}
and this is the c# code:
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
try
{
UploadFile(dlg.File.Name, dlg.File.OpenRead());
titlu.Text = dlg.File.Name;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
titlu.Text = "No file selected...";
}
}
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://ganduri.elementfx.com/Handler.ashx");
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
MessageBox.Show("Writed");
}
I have also checked the permissions to the ClientBin folder and are read/write permissions
When I run this code it shows me the "Writed" messageBox, but if I look in the server, the file isn't anywhere.
I have set a breakpoint in af ashx file functions and id doesn't reach any of them.
Think you need check upload folder for existence, and write access rights