本文整理汇总了C#中System.Windows.Media.Imaging.WriteableBitmap.SetSource方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.SetSource方法的具体用法?C# WriteableBitmap.SetSource怎么用?C# WriteableBitmap.SetSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Media.Imaging.WriteableBitmap
的用法示例。
在下文中一共展示了WriteableBitmap.SetSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LimitImageSize
private static Stream LimitImageSize(Stream imageStream, double imageMaxDiagonalSize)
{
// In order to determine the size of the image we will transfer it to a writable bitmap.
WriteableBitmap wb = new WriteableBitmap(1, 1);
wb.SetSource(imageStream);
// Check if we need to scale it down.
double imageDiagonalSize = Math.Sqrt(wb.PixelWidth * wb.PixelWidth + wb.PixelHeight * wb.PixelHeight);
if (imageDiagonalSize > imageMaxDiagonalSize)
{
// Calculate the new image size that corresponds to imageMaxDiagonalSize for the
// diagonal size and that preserves the aspect ratio.
int newWidth = (int)(wb.PixelWidth * imageMaxDiagonalSize / imageDiagonalSize);
int newHeight = (int)(wb.PixelHeight * imageMaxDiagonalSize / imageDiagonalSize);
Stream resizedStream = new MemoryStream();
Extensions.SaveJpeg(wb, resizedStream, newWidth, newHeight, 0, 100);
return resizedStream;
}
else
{
// No need to scale down. The image diagonal is less than or equal to imageMaxSizeDiagonal.
return imageStream;
}
}
示例2: Button_Click_1
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += (ee,s)=>
{
//DirectX context should be recreate before cereate the texture
Dispatcher.BeginInvoke(() =>
{
WriteableBitmap bmp = new WriteableBitmap(1,1);
bmp.SetSource(s.ChosenPhoto);
m_d3dInterop.CreateTexture(bmp.Pixels, bmp.PixelWidth, bmp.PixelHeight);
MessageBox.Show("Picture loaded with c#");
});
};
photoChooserTask.Show();
}
catch(Exception exp)
{
}
}
示例3: resizeImage
// call by reference
public static void resizeImage(ref WriteableBitmap bmp)
{
// TODO: memory management
// we have 2 options
// i) use "using" statement
// ii) dispose of object "ms" before the method finishes (**check bmp1 as ms is set as it's source )
MemoryStream ms = new MemoryStream();
int h, w;
if (bmp.PixelWidth > bmp.PixelHeight)
{
double aspRatio = bmp.PixelWidth / (double)bmp.PixelHeight;
double hh, ww;
hh = (640.0 / aspRatio);
ww = hh * aspRatio;
h = (int)hh;
w = (int)ww;
}
else
{
double aspRatio = bmp.PixelHeight / (double)bmp.PixelWidth;
double hh, ww;
hh = (480.0 / aspRatio);
ww = hh * aspRatio;
h = (int)hh;
w = (int)ww;
}
bmp.SaveJpeg(ms, w, h, 0, 100);
bmp.SetSource(ms);
}
示例4: task_Completed
void task_Completed(object sender, PhotoResult e)
{
if(e.TaskResult == TaskResult.OK)
{
var bmp = new WriteableBitmap(0,0);
bmp.SetSource(e.ChosenPhoto);
var bmpres = new WriteableBitmap(bmp.PixelWidth,bmp.PixelHeight);
var txt = new WindowsPhoneControl1();
txt.Text = txtInput.Text;
txt.FontSize = 36 * bmpres.PixelHeight / 480;
txt.Width = bmpres.PixelWidth;
txt.Height = bmpres.PixelHeight;
//should call Measure and when uicomponent is not in visual tree
txt.Measure(new Size(bmpres.PixelWidth, bmpres.PixelHeight));
txt.Arrange(new Rect(0, 0, bmpres.PixelWidth, bmpres.PixelHeight));
bmpres.Render(new Image() { Source = bmp }, null);
bmpres.Render(txt, null);
bmpres.Invalidate();
display.Source = bmpres;
}
}
示例5: assertCorrectImage2result
private static void assertCorrectImage2result(String path, ExpandedProductParsedResult expected)
{
RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
if (!File.Exists(path))
{
// Support running from project root too
path = Path.Combine("..\\..\\..\\Source", path);
}
#if !SILVERLIGHT
var image = new Bitmap(Image.FromFile(path));
#else
var image = new WriteableBitmap(0, 0);
image.SetSource(File.OpenRead(path));
#endif
BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BitmapLuminanceSource(image)));
int rowNumber = binaryMap.Height / 2;
BitArray row = binaryMap.getBlackRow(rowNumber, null);
Result theResult = rssExpandedReader.decodeRow(rowNumber, row, null);
Assert.IsNotNull(theResult);
Assert.AreEqual(BarcodeFormat.RSS_EXPANDED, theResult.BarcodeFormat);
ParsedResult result = ResultParser.parseResult(theResult);
Assert.AreEqual(expected, result);
}
示例6: OnNavigatedTo
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ApplicationBar.Opacity = 0.5;
if (this.NavigationContext.QueryString.ContainsKey("nid"))
{
var nid = Int32.Parse(this.NavigationContext.QueryString["nid"]);
Capitulo c;
if (AU.Instance.Capitulos.TryGetValue(nid, out c))
{
PageTitle.Text = c.Nombre;
sinopsis.Text = c.TextoNoti;
textAutor.Text = c.NotiAutor;
textFecha.Text = c.ReleaseDate.ToString();
var i = 0;
serieId = c.Serie;
foreach (String file in c.Imagenes)
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(file))
{
using (var imageStream = myIsolatedStorage.OpenFile(file, FileMode.Open, FileAccess.Read))
{
WriteableBitmap temp = new WriteableBitmap(0, 0);
try
{
Image image1 = new Image();
WriteableBitmap aux = new WriteableBitmap(0, 0);
aux.SetSource(imageStream);
image1.Source = (ImageSource)aux;
image1.HorizontalAlignment = HorizontalAlignment.Left;
image1.VerticalAlignment = VerticalAlignment.Top;
image1.Stretch = Stretch.Uniform;
ColumnDefinition colDef3 = new ColumnDefinition();
imagenes.ColumnDefinitions.Add(colDef3);
Grid.SetColumn(image1, i);
i++;
imagenes.Children.Add(image1);
}
catch (ArgumentException ae)
{
}
}
}
}
}
}
}
else
{
// I use this condition to handle creating new items.
}
}
示例7: toWriteableBitmap
public static WriteableBitmap toWriteableBitmap(Stream stream, int pixelWidth, int pixelHeight) {
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
WriteableBitmap bitmap = new WriteableBitmap(pixelWidth, pixelWidth);
bitmap.SetSource(stream);
return bitmap;
}
示例8: BytesToImage
public BitmapImage BytesToImage(byte[] array, int width, int height)
{
WriteableBitmap result = new WriteableBitmap(width, height);
MemoryStream ms = new MemoryStream();
ms.Write(array, 0, array.Length);
result.SetSource(ms);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
示例9: ReadImageFromIsolatedStorage
public static WriteableBitmap ReadImageFromIsolatedStorage(string imageName, int width, int height)
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fileStream = isoStore.OpenFile(imageName, FileMode.Open, FileAccess.Read))
{
var bitmap = new WriteableBitmap(width, height);
bitmap.SetSource(fileStream);
return bitmap;
}
}
}
示例10: ToBitmapImageAsync
public async static Task<WriteableBitmap> ToBitmapImageAsync(this Stream imageStream, Tuple<int, int> downscale, bool useDipUnits, InterpolationMode mode, ImageInformation imageInformation = null)
{
if (imageStream == null)
return null;
using (IRandomAccessStream image = new RandomStream(imageStream))
{
if (downscale != null && (downscale.Item1 > 0 || downscale.Item2 > 0))
{
using (var downscaledImage = await image.ResizeImage(downscale.Item1, downscale.Item2, mode, useDipUnits, imageInformation).ConfigureAwait(false))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(downscaledImage);
downscaledImage.Seek(0);
WriteableBitmap resizedBitmap = null;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
resizedBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var s = downscaledImage.AsStream())
{
resizedBitmap.SetSource(s);
}
});
return resizedBitmap;
}
}
else
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0);
WriteableBitmap bitmap = null;
if (imageInformation != null)
{
imageInformation.SetCurrentSize((int)decoder.PixelWidth, (int)decoder.PixelHeight);
imageInformation.SetOriginalSize((int)decoder.PixelWidth, (int)decoder.PixelHeight);
}
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
bitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
bitmap.SetSource(imageStream);
});
return bitmap;
}
}
}
示例11: CreateDeadTileBitmap
public void CreateDeadTileBitmap()
{
// load your image
StreamResourceInfo info = Application.GetResourceStream(new Uri(DEAD_TILE, UriKind.Relative));
// create source bitmap for Image control (image is assumed to be alread 173x173)
WriteableBitmap wbmp = new WriteableBitmap(173, 173);
wbmp.SetSource(info.Stream);
// save image to isolated storage
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
// use of "/Shared/ShellContent/" folder is mandatory!
using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/Tile.jpg", System.IO.FileMode.Create, isf))
{
wbmp.SaveJpeg(imageStream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
}
}
}
示例12: loadImage
private static WriteableBitmap loadImage(String fileName)
#endif
{
String file = BASE_IMAGE_PATH + fileName;
if (!File.Exists(file))
{
// try starting with 'core' since the test base is often given as the project root
file = "..\\..\\..\\Source\\" + BASE_IMAGE_PATH + fileName;
}
Assert.IsTrue(File.Exists(file), "Please run from the 'core' directory");
#if !SILVERLIGHT
return (Bitmap)Bitmap.FromFile(file);
#else
var wb = new WriteableBitmap(0, 0);
wb.SetSource(File.OpenRead(file));
return wb;
#endif
}
示例13: ToBitmapImageAsync
public async static Task<WriteableBitmap> ToBitmapImageAsync(this byte[] imageBytes, Tuple<int, int> downscale, InterpolationMode mode)
{
if (imageBytes == null)
return null;
using (var imageStream = imageBytes.AsBuffer().AsStream())
using (IRandomAccessStream image = new RandomStream(imageStream))
{
if (downscale != null && (downscale.Item1 > 0 || downscale.Item2 > 0))
{
using (var downscaledImage = await image.ResizeImage((uint)downscale.Item1, (uint)downscale.Item2, mode).ConfigureAwait(false))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(downscaledImage);
downscaledImage.Seek(0);
WriteableBitmap resizedBitmap = null;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
resizedBitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var s = downscaledImage.AsStream())
{
resizedBitmap.SetSource(s);
}
});
return resizedBitmap;
}
}
else
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0);
WriteableBitmap bitmap = null;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
bitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
bitmap.SetSource(imageStream);
});
return bitmap;
}
}
}
示例14: testFindFinderPatterns
public void testFindFinderPatterns()
{
RSSExpandedReader rssExpandedReader = new RSSExpandedReader();
String path = "test/data/blackbox/rssexpanded-1/2.png";
if (!File.Exists(path))
{
// Support running from project root too
path = Path.Combine("..\\..\\..\\Source", path);
}
#if !SILVERLIGHT
var image = new Bitmap(Image.FromFile(path));
#else
var image = new WriteableBitmap(0, 0);
image.SetSource(File.OpenRead(path));
#endif
BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BitmapLuminanceSource(image)));
int rowNumber = binaryMap.Height / 2;
BitArray row = binaryMap.getBlackRow(rowNumber, null);
List<ExpandedPair> previousPairs = new List<ExpandedPair>();
ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
previousPairs.Add(pair1);
FinderPattern finderPattern = pair1.FinderPattern;
Assert.IsNotNull(finderPattern);
Assert.AreEqual(0, finderPattern.Value);
ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
previousPairs.Add(pair2);
finderPattern = pair2.FinderPattern;
Assert.IsNotNull(finderPattern);
Assert.AreEqual(1, finderPattern.Value);
ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber);
previousPairs.Add(pair3);
finderPattern = pair3.FinderPattern;
Assert.IsNotNull(finderPattern);
Assert.AreEqual(1, finderPattern.Value);
// the previous was the last pair
Assert.IsNull(rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber));
}
示例15: btnDecoderOpen_Click
private void btnDecoderOpen_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Filter = "PNG (*.png)|*.png";
if (dlg.ShowDialog().GetValueOrDefault(false))
{
try
{
currentBarcode = new WriteableBitmap(0, 0);
using (var stream = dlg.File.OpenRead())
{
currentBarcode.SetSource(stream);
}
imgDecoderBarcode.Source = currentBarcode;
}
catch (Exception exc)
{
txtDecoderContent.Text = exc.Message;
}
}
}