The following example shows how to use RenderTargetBitmap to render text as an image.
| C# |
Copy Code |
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Globalization;
namespace SDKSample
{
public partial class RenderTargetBitmapExample : Page
{
public RenderTargetBitmapExample()
{
Image myImage = new Image();
FormattedText text = new FormattedText("ABC",
new CultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()),
this.FontSize,
this.Foreground);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawText(text, new Point(2, 2));
drawingContext.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
myImage.Source = bmp;
// Add Image to the UI
StackPanel myStackPanel = new StackPanel();
myStackPanel.Children.Add(myImage);
this.Content = myStackPanel;
}
}
}
|
using System;
using System.Collections.Generic;
using System.Text;
namespace MATRIX
{
class MatrixX
{
private double[,] m1 = new double[,] { { 1, 2, 3, 4 }, { 2.5, 5, 7, 90 }, { 45, 3.2, 2.5, 320.1 }, { 4587.2, 6542.8, 236.015, 523.57 } };
private double[,] m2 = new double[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
static void Main(string[] args)
{
MatrixX mx= new MatrixX();
DateTime start = DateTime.Now;
mx.MatrixXXpointer();
DateTime end = DateTime.Now;
TimeSpan ts = end - start;
Console.WriteLine(ts.TotalMilliseconds);
Console.Read();
}
public void MatrixXX()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
double x = 0;
for (int k = 0; k < 4; k++)
{
x = x + m1[i, k] * m1[k, j];
}
m2[i, j] = x;
}
}
public unsafe void MatrixXXpointer()
{
double*[] S = new double*[4];
double*[] t = new double*[4];
for (int i = 0; i < 4; i++)
{
double* pt = stackalloc double[4];
double* pt1 = stackalloc double[4];
S[i] = pt;
t[i] = pt1;
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
*(S[i] + j) = m1[i, j];
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
double x = 0.0;
for (int k = 0; k < 4; k++)
{
x = x + *(S[i] + k) * *(S[k] + j);
}
*(t[i]+ j) = x;
}
}
}
}
使用心得1:
c#中可以使用指针数组代替c语言中的二维数组。
使用心得2:
c#中的指针生命周期仅限于为指针赋值的函数体,当函数返回时,指针的行为是不可预料的。
具体代码为:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices;
namespace sand.shilin.ModuleMatching
{
public unsafe class ModuleMatchingFaster
{
public ModuleMatchingFaster()
{
}
public unsafe static int[] MatchingFast()
{
int[] MatchedLocation = new int[2];
Bitmap s = global::sand.shilin.ModuleMatching.Properties.Resources.lena2562;
byte*[] S = new byte*[s.Height];
for (int i = 0; i < s.Height; i++)
{
byte* pt = stackalloc byte[s.Width];
S[i] = pt;
}
Image2Ptr(S, s);
int sums = sum(s, S);
s = global::sand.shilin.ModuleMatching.Properties.Resources.lenaT;
byte*[] T = new byte*[s.Height];
for (int i = 0; i < s.Height; i++)
{
byte* pt = stackalloc byte[s.Width];
T[i] = pt;
}
Image2Ptr(T, s);
int sumT = sum(s, T);
int f = Convert.ToInt32(sumT * 0.1);
int sumWeldingH = sums;
int sumWelding = sums;
int avrSource = sums / (35 * 44);
int avrModule = sumT / (35 * 44);
int*[] D = new int*[221];
for (int i = 0; i < 212; i++)
{
int* pt = stackalloc int[221];
D[i] = pt;
}
for (int i = 0; i < 212; i++)
{
for (int j = 0; j < 221; j++)
{
int wucha = 0;
bool breaks = false;
int k = 0;
while (breaks == false && k < 44)
{
for (int l = 0; l < 35; l+=1)
{
wucha += Math.Abs(*(S[k + i] + l + j) - avrSource - *(T[k] + l) + avrModule);
if (wucha >= f)
{
breaks = true;
break;
}
}
k += 1;
}
int hang = 0;
for (int m = 0; m < 44; m++)
{
hang = hang - *(S[i + m] + j) + *(S[i + m] + j + 35 - 1);
}
sumWelding = sumWelding + hang;
avrSource = sumWelding / (35 * 44);
if (wucha < f)
{
f = wucha;
}
*(D[i] + j) = wucha;
}
int lie = 0;
for (int n = 0; n < 35; n++)
{
lie += *(S[i + 35 - 1] + n) - *(S[i] + n);
}
sumWeldingH = sumWeldingH + lie;
sumWelding = sumWeldingH;
avrSource = sumWeldingH / (35 * 44);
}
int min = *(D[0]);
int p1 = 0;
int p2 = 0;
for (int i = 0; i < 212; i++)
{
for (int j = 0; j < 221; j++)
{
if (*(D[i] + j) < min)
{
min = *(D[i] + j);
p1 = i;
p2 = j;
}
}
}
int mmmm = min;
MatchedLocation[0] = p1;
MatchedLocation[1] = p2;
return MatchedLocation;
}
unsafe private static void Image2Ptr(byte*[] S, Bitmap s)
{
BitmapData sData = s.LockBits(new Rectangle(0, 0, s.Width, s.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int sstride = sData.Stride;
System.IntPtr sScan0 = sData.Scan0;
int sOffset = sstride - s.Width * 3;
byte* sp = (byte*)(void*)sScan0;
for (int i = 0; i < s.Height; i++)
{
for (int j = 0; j < s.Width; j++)
{
*(S[i] + j) = sp[0];
sp += 3;
}
sp += sOffset;
}
s.UnlockBits(sData);
}
unsafe private static int sum(Bitmap s, byte*[] S)
{
int sum = 0;
for (int i = 0; i < 44; i++)
{
for (int j = 0; j < 35; j++)
{
sum += *(S[i] + j);
}
}
return sum;
}
}
}
这是一个成功的代码。
以下是一个不成功的代码。
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace sand.shilin.ModuleMatching
{
[Obsolete]
unsafe public class ModuleMatching
{
Bitmap s = global::sand.shilin.ModuleMatching.Properties.Resources.lena2562;
Bitmap t = global::sand.shilin.ModuleMatching.Properties.Resources.lenaT;
byte*[] ptrSource;
byte*[] ptrModule;
public ModuleMatching()
{
ptrModule = new byte*[t.Height];
for (int i = 0; i < t.Height; i++)
{
byte* pt = stackalloc byte[t.Width];
ptrModule[i] = pt;
}
ptrSource = new byte*[s.Height];
for (int i = 0; i < s.Height; i++)
{
byte* pt = stackalloc byte[s.Width];
ptrSource[i] = pt;
}
CreatePtr(t, ptrModule);
//int[,] MODULE = new int[44, 35];
//for (int i = 0; i < 44; i++)
// for (int j = 0; j < 35; j++)
// MODULE[i, j] = *(ptrModule[i] + j);
}
unsafe public int[] MatchingFast()
{
int[] MatchedLocation = new int[2];
CreatePtr(s, ptrSource);
int sums = sum(ptrSource);
int sumT = sum(ptrModule);
int f = Convert.ToInt32(sumT);
int sumWeldingH = sums;
int sumWelding = sums;
int avrSource = sums / (t.Width * t.Height);
int avrModule = sumT / (t.Width * t.Height);
int*[] D = new int*[s.Height - t.Height];
for (int i = 0; i < s.Height - t.Height; i++)
{
int* pt = stackalloc int[s.Height - t.Height];
D[i] = pt;
}
for (int i = 0; i < s.Height - t.Height; i++)
{
for (int j = 0; j < s.Width - t.Width; j++)
{
int wucha = 0;
bool breaks = false;
int k = 0;
while (breaks == false && k < t.Height)
{
for (int l = 0; l < t.Width; l += 1)
{
wucha += Math.Abs(*(ptrSource[k + i] + l + j) - avrSource - *(ptrModule[k] + l) + avrModule);
if (wucha >= f)
{
breaks = true;
break;
}
}
k += 1;
}
int hang = 0;
for (int m = 0; m < t.Height; m++)
{
hang = hang - *(ptrSource[i + m] + j) + *(ptrSource[i + m] + j + t.Width - 1);
}
sumWelding = sumWelding + hang;
avrSource = sumWelding / (t.Width * t.Height);
if (wucha < f)
{
f = wucha;
}
*(D[i] + j) = wucha;
}
int lie = 0;
for (int n = 0; n < t.Width; n++)
{
lie += *(ptrSource[i + t.Width - 1] + n) - *(ptrSource[i] + n);
}
sumWeldingH = sumWeldingH + lie;
sumWelding = sumWeldingH;
avrSource = sumWeldingH / (t.Width * t.Height);
}
int min = *(D[0]);
int p1 = 0;
int p2 = 0;
for (int i = 0; i < s.Height - t.Height; i++)
{
for (int j = 0; j < s.Width - t.Width; j++)
{
if (*(D[i] + j) < min)
{
min = *(D[i] + j);
p1 = i;
p2 = j;
}
}
}
int mmmm = min;
MatchedLocation[0] = p1;
MatchedLocation[1] = p2;
return MatchedLocation;
}
unsafe private void CreatePtr(Bitmap s, byte*[] p)
{
BitmapData sData = s.LockBits(new Rectangle(0, 0, s.Width, s.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
int sstride = sData.Stride;
System.IntPtr sScan0 = sData.Scan0;
int sOffset = sstride - s.Width * 3;
byte* sp = (byte*)(void*)sScan0;
for (int i = 0; i < s.Height; i++)
{
for (int j = 0; j < s.Width; j++)
{
*(p[i] + j) = sp[0];
sp += 3;
}
sp += sOffset;
}
s.UnlockBits(sData);
}
unsafe private int sum(byte*[] p)
{
int sum = 0;
for (int i = 0; i < t.Height; i++)
{
for (int j = 0; j < t.Width; j++)
{
sum += *(p[i] + j);
}
}
return sum;
}
}
}
在构造函数返回后,ptrModule的值变得不可预料,在MatchingFast中,ptrModule已经变得不可用。
企业库2.0安全应用程序块使用(1)
第一次使用企业库,不当之处,请高手指点。
一、添加对Caching Common Security objectBuilder四个dll的引用
二、完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Security;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Security;
using System.Security.Principal;
using System.Web.Security;
using System.Configuration.Provider;
namespace SecurityApp
{
public partial class Form1 : Form
{
ISecurityCacheProvider cache;
IIdentity identity = null;
IToken token = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//cache = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider");
//if (Membership.ValidateUser("Username", "password"))
//{
// identity = new GenericIdentity("username", Membership.Provider.Name);
//}
//if (identity != null)
//{
// token = cache.SaveIdentity(identity);
//}
IPrincipal principal = new GenericPrincipal(new GenericIdentity("uame"), new string[] { "Manager" });
IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider("RuleProvider");
// Determine whether user is authorized for the rule defined as "Print Document".
bool authorized = ruleProvider.Authorize(principal, "FullAccessToDatabase");
if (authorized)
MessageBox.Show("UserName Have the full access to database");
else
MessageBox.Show("UserName have no access to database");
}
}
}
三、配置文件中
添加安全应用程序块,在RuleProvider上新建一个规则,明明为FullAccessToDatabase,其value为I: Username。
如果程序中的GenericIdentity的参数为Username,则通过认证。否则 程序输出UserName have no access to database。