本文整理汇总了C#中SharpDX.DirectWrite.TextFormat.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# TextFormat.Dispose方法的具体用法?C# TextFormat.Dispose怎么用?C# TextFormat.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SharpDX.DirectWrite.TextFormat
的用法示例。
在下文中一共展示了TextFormat.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateTextSprite
//.........这里部分代码省略.........
: (CCVerticalTextAlignment.Top == verticleAlignement) ? 0 // align to top
: (imageHeight - boundingRect.Bottom) * 0.5f; // align to center
SharpDX.WIC.Bitmap sharpBitmap = null;
WicRenderTarget sharpRenderTarget = null;
SolidColorBrush solidBrush = null;
try
{
// Select our pixel format
var pixelFormat = SharpDX.WIC.PixelFormat.Format32bppPRGBA;
// create our backing bitmap
sharpBitmap = new SharpDX.WIC.Bitmap(FactoryImaging, imageWidth, imageHeight, pixelFormat, BitmapCreateCacheOption.CacheOnLoad);
// Create the render target that we will draw to
sharpRenderTarget = new WicRenderTarget(Factory2D, sharpBitmap, new RenderTargetProperties());
// Create our brush to actually draw with
solidBrush = new SolidColorBrush(sharpRenderTarget, foregroundColor);
// Begin the drawing
sharpRenderTarget.BeginDraw();
if (textDefinition.isShouldAntialias)
sharpRenderTarget.AntialiasMode = AntialiasMode.Aliased;
// Clear it
sharpRenderTarget.Clear(TransparentColor);
// Draw the text to the bitmap
sharpRenderTarget.DrawTextLayout(new Vector2(boundingRect.X, yOffset), textLayout, solidBrush);
// End our drawing which will commit the rendertarget to the bitmap
sharpRenderTarget.EndDraw();
// Debugging purposes
//var s = "Label4";
//SaveToFile(@"C:\Xamarin\" + s + ".png", _bitmap, _renderTarget);
// The following code creates a .png stream in memory of our Bitmap and uses it to create our Textue2D
Texture2D tex = null;
using (var memStream = new MemoryStream())
{
using (var encoder = new PngBitmapEncoder(FactoryImaging, memStream))
using (var frameEncoder = new BitmapFrameEncode(encoder))
{
frameEncoder.Initialize();
frameEncoder.WriteSource(sharpBitmap);
frameEncoder.Commit();
encoder.Commit();
}
// Create the Texture2D from the png stream
tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, memStream);
}
// Return our new CCTexture2D created from the Texture2D which will have our text drawn on it.
return new CCTexture2D(tex);
}
catch (Exception exc)
{
CCLog.Log("CCLabel-Windows: Unable to create the backing image of our text. Message: {0}", exc.StackTrace);
}
finally
{
if (sharpBitmap != null)
{
sharpBitmap.Dispose();
sharpBitmap = null;
}
if (sharpRenderTarget != null)
{
sharpRenderTarget.Dispose();
sharpRenderTarget = null;
}
if (solidBrush != null)
{
solidBrush.Dispose();
solidBrush = null;
}
if (textFormat != null)
{
textFormat.Dispose();
textFormat = null;
}
if (textLayout != null)
{
textLayout.Dispose();
textLayout = null;
}
}
// If we have reached here then something has gone wrong.
return new CCTexture2D();
}
示例2: CreateTextSprite
//.........这里部分代码省略.........
}
var fontName = font.FontFamily.FamilyNames.GetString(0);
var textFormat = new TextFormat(FactoryDWrite, fontName,
_currentFontCollection, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, _currentDIP);
textFormat.TextAlignment = textAlign;
textFormat.ParagraphAlignment = paragraphAlign;
var textLayout = new TextLayout(FactoryDWrite, text, textFormat, dimensions.Width, dimensions.Height);
var boundingRect = new RectangleF();
// Loop through all the lines so we can find our drawing offsets
var textMetrics = textLayout.Metrics;
var lineCount = textMetrics.LineCount;
// early out if something went wrong somewhere and nothing is to be drawn
if (lineCount == 0)
return new CCTexture2D();
// Fill out the bounding rect width and height so we can calculate the yOffset later if needed
boundingRect.X = 0;
boundingRect.Y = 0;
boundingRect.Width = textMetrics.Width;
boundingRect.Height = textMetrics.Height;
if (!layoutAvailable)
{
if (dimensions.Width == 8388608)
{
dimensions.Width = boundingRect.Width;
}
if (dimensions.Height == 8388608)
{
dimensions.Height = boundingRect.Height;
}
}
imageWidth = (int)dimensions.Width;
imageHeight = (int)dimensions.Height;
// Recreate our layout based on calculated dimensions so that we can draw the text correctly
// in our image when Alignment is not Left.
if (textAlign != TextAlignment.Leading)
{
textLayout.MaxWidth = dimensions.Width;
textLayout.MaxHeight = dimensions.Height;
}
// Line alignment
var yOffset = (CCVerticalTextAlignment.Bottom == verticleAlignement
|| boundingRect.Bottom >= dimensions.Height) ? dimensions.Height - boundingRect.Bottom // align to bottom
: (CCVerticalTextAlignment.Top == verticleAlignement) ? 0 // align to top
: (imageHeight - boundingRect.Bottom) * 0.5f; // align to center
if (labelRenderer == null)
labelRenderer = new CCLabel_Renderer81();
try
{
// The following code creates a .png stream in memory of our Bitmap and uses it to create our Textue2D
Texture2D tex = null;
using (var pngStream = labelRenderer.RenderLabelToStream(imageWidth, imageHeight, foregroundColor,
new Vector2(boundingRect.X, yOffset), textLayout).Result)
{
// Create the Texture2D from the png stream
tex = Texture2D.FromStream(CCDrawManager.SharedDrawManager.XnaGraphicsDevice, pngStream);
}
// Return our new CCTexture2D created from the Texture2D which will have our text drawn on it.
return new CCTexture2D(tex);
}
catch (Exception exc)
{
CCLog.Log("CCLabel-Windows: Unable to create the backing image of our text. Message: {0}", exc.StackTrace);
}
finally
{
if (textFormat != null)
{
textFormat.Dispose();
textFormat = null;
}
if (textLayout != null)
{
textLayout.Dispose();
textLayout = null;
}
}
// If we have reached here then something has gone wrong.
return new CCTexture2D();
}
示例3: Main
//.........这里部分代码省略.........
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// EFFECT SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Effect 1 : BitmapSource - take decoded image data and get a BitmapSource from it
var bitmapSourceEffect = new d2.Effects.BitmapSource(d2dContext);
bitmapSourceEffect.WicBitmapSource = formatConverter;
// Effect 2 : GaussianBlur - give the bitmapsource a gaussian blurred effect
var gaussianBlurEffect = new d2.Effects.GaussianBlur(d2dContext);
gaussianBlurEffect.SetInput(0, bitmapSourceEffect.Output, true);
gaussianBlurEffect.StandardDeviation = 5f;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// OVERLAY TEXT SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var textFormat = new dw.TextFormat(dwFactory, "Arial", 15f); // create the text format of specified font configuration
// draw a long text to show the automatic line wrapping
var textToDraw = "Some long text to show the drawing of preformatted "
+ "glyphs using DirectWrite on the Direct2D surface."
+ " Notice the automatic wrapping of line if it exceeds desired width.";
// create the text layout - this improves the drawing performance for static text
// as the glyph positions are precalculated
var textLayout = new dw.TextLayout(dwFactory, textToDraw, textFormat, 300f, 1000f);
var textBrush = new d2.SolidColorBrush(d2dContext, Color.LightGreen);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RENDER TARGET SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// create the d2d bitmap description using default flags (from SharpDX samples) and 96 DPI
var d2dBitmapProps = new d2.BitmapProperties1(d2PixelFormat, 96, 96, d2.BitmapOptions.Target | d2.BitmapOptions.CannotDraw);
// the render target
var d2dRenderTarget = new d2.Bitmap1(d2dContext, new Size2(pixelWidth, pixelHeight), d2dBitmapProps);
d2dContext.Target = d2dRenderTarget; // associate bitmap with the d2d context
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// DRAWING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// slow preparations - fast drawing:
d2dContext.BeginDraw();
d2dContext.DrawImage(gaussianBlurEffect);
d2dContext.DrawTextLayout(new Vector2(5f, 5f), textLayout, textBrush);
d2dContext.EndDraw();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// IMAGE SAVING ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// delete the output file if it already exists
if (System.IO.File.Exists(outputPath)) System.IO.File.Delete(outputPath);
// use the appropiate overload to write either to stream or to a file
var stream = new wic.WICStream(imagingFactory, outputPath, NativeFileAccess.Write);
// select the image encoding format HERE
var encoder = new wic.PngBitmapEncoder(imagingFactory);
encoder.Initialize(stream);
var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder);
bitmapFrameEncode.Initialize();
bitmapFrameEncode.SetSize(pixelWidth, pixelHeight);
bitmapFrameEncode.SetPixelFormat(ref wicPixelFormat);
// this is the trick to write D2D1 bitmap to WIC
var imageEncoder = new wic.ImageEncoder(imagingFactory, d2dDevice);
imageEncoder.WriteFrame(d2dRenderTarget, bitmapFrameEncode, new wic.ImageParameters(d2PixelFormat, 96, 96, 0, 0, pixelWidth, pixelHeight));
bitmapFrameEncode.Commit();
encoder.Commit();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CLEANUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// dispose everything and free used resources
bitmapFrameEncode.Dispose();
encoder.Dispose();
stream.Dispose();
textBrush.Dispose();
textLayout.Dispose();
textFormat.Dispose();
formatConverter.Dispose();
gaussianBlurEffect.Dispose();
bitmapSourceEffect.Dispose();
d2dRenderTarget.Dispose();
inputStream.Dispose();
decoder.Dispose();
d2dContext.Dispose();
dwFactory.Dispose();
imagingFactory.Dispose();
d2dDevice.Dispose();
dxgiDevice.Dispose();
d3dDevice.Dispose();
defaultDevice.Dispose();
// show the result
System.Diagnostics.Process.Start(outputPath);
}