顯示具有 c# 標籤的文章。 顯示所有文章
顯示具有 c# 標籤的文章。 顯示所有文章

星期二, 9月 07, 2010

[Silverlight] 將畫面上元件輸出為圖片

VB:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim bitmap As WriteableBitmap = New WriteableBitmap(can, Nothing)

        If Not IsNothing(bitmap) Then
            Dim savedia As SaveFileDialog = New SaveFileDialog()
            savedia.Filter = "JPEG Files (*.jpeg)|*.jpeg"
            savedia.DefaultExt = ".jpeg"

            If savedia.ShowDialog() Then
                Using st As System.IO.Stream = savedia.OpenFile()
                    SaveToFile(bitmap, st)
                    MessageBox.Show("done")
                End Using
            End If
        End If

    End Sub

    Private Shared Sub SaveToFile(ByVal bitmap As WriteableBitmap, ByVal st As System.IO.Stream)
        Dim wid As Integer = bitmap.PixelWidth
        Dim hei As Integer = bitmap.PixelHeight
        Dim bands As Integer = 2
        Dim raster()(,) As Byte = New Byte(bands)(,) {}

        For index = 0 To bands
            raster(index) = New Byte(wid, hei) {}

        Next

        For row = 0 To hei - 1

            For col = 0 To wid - 1
                Dim pixel As Integer = bitmap.Pixels(wid * row + col)
                Dim tmp As Integer

                tmp = (pixel >> 16) Mod 256
                raster(0)(col, row) = (256 + tmp) Mod 256
                tmp = (pixel >> 8) Mod 256
                raster(1)(col, row) = (256 + tmp) Mod 256
                tmp = pixel Mod 256
                raster(2)(col, row) = (256 + tmp) Mod 256

            Next

        Next
        Dim model As ColorModel = New ColorModel() With {.colorspace = ColorSpace.RGB}
        Dim img As FluxJpeg.Core.Image = New FluxJpeg.Core.Image(model, raster)

        Using ms As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim encoder As FluxJpeg.Core.Encoder.JpegEncoder = New FluxJpeg.Core.Encoder.JpegEncoder(img, 100, ms)
            encoder.Encode()
            ms.Seek(0, IO.SeekOrigin.Begin)
            Dim binary() As Byte = New Byte(ms.Length) {}
            Dim byteread = ms.Read(binary, 0, ms.Length)
            st.Write(binary, 0, binary.Length)
        End Using

    End Sub

C#:網路找就有了...差異較大的地方就是轉byte,VB CByte很蠢就是了...
此外要去抓fjCore dll才能使用

星期四, 3月 11, 2010

十六進位表示法

不同電腦系統、編程語言對於16進制數值有不同的表示方式:

* Ada用所基於的「數字引證」把16進位數包起來,例如「16#5A3#」。(註:Ada對整數和實數都可以使用從1到16中任何一個做為其基數。)
* VHDL中使用字首 X,例如,X"10",對應的二進位碼為:"00010000".
* C語言、Python語言及其他相近的語言使用字首「0x」,例如「0x5A3」。開頭的「0」令解析器更易辨認數,而「x」則代表十六進位(就如「O」代表八進位)。在「0x」中的「x」可以大寫或小寫
* 在VB中,使用字首「&H」來表示
* 在HTML,十六進位字元可以用「x」,例如֣和֣效果應該無異
* 某些版本的BASIC語言會採用「&H」詞頭,例如:「&H5A3」。
* Pascal和某些匯編器用字尾「h」來辨別16進位的數(若該數以字母起首,在前面會增加一個「0」)例如「0A3Ch」、「5A3h」
* 其他匯編器(AT&T、Motorola)和某些BASIC的版本使用字首「$」,例如「$5A3」
* 亦有用X'5A3'這類表示方式的,如PL/I

由於表示方式沒有單一、已協定的標準,所以以上的表示方式都有被使用,有時甚至在同一份論文上會出現幾個不同的表示方式。但因為各方式之間沒有太大的分歧,所以即使混合使用也沒有構成問題。

其中最常用(或常見)表示十六進位數值的方式是將 '0x' 加在數字前,或在數字後加上小字 16。例如 0x2BAD 和 2BAD16 都是表示十進位的11181(或1118110)。

在網頁設計上十六進制是很常用的。HTML和CSS使用十六進制的表示法來表示網頁上的特定顏色。使用 # 的符號來表示而非用個別的符號表示十六進制。24-bit 顏色可以用 #RRGGBB 的格式來表示,RR 是顏色中紅色成分的數值,GG 是顏色中綠色成分的數值,BB 顏色中藍色成分的數值。舉個例子, 紅色的陰影是十進制 238,9,63 可以編成 #EE093F。參見 網頁顏色模式.

在URL的特殊字元亦是用ASCII中的十六進位,每位元組都有百份比符號(%)在前,例如: 空格就表示為%20,而中文維基百科的首頁地址就是 http://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5 (在一些現代的瀏覽器,如Firefox中,地址中十六進位編碼會被解碼成實際字元,所以在這些瀏覽器中中文維基百科的首頁地址顯示成 http://zh.wikipedia.org/wiki/Wikipedia:首頁 )

轉至Wiki

星期二, 3月 02, 2010

GetURl

public class GetUrl
{
public static string GetAbsoluteUrl(string strRelativePath)
{
if (string.IsNullOrEmpty(strRelativePath))
return strRelativePath;

string strFullUrl;
if (strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
)
{
// 已經是絕對路徑。
strFullUrl = strRelativePath;
}
else
{
// 是相對路徑,必須轉換成絕對路徑。
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri;
if (strFullUrl.IndexOf("ClientBin") > 0)
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath;
else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath;
}

return strFullUrl;
}
}

===============================================
Public Class GetUrl
Public Shared Function GetAbsoluteUrl(ByVal strRelativePath As String) As String
Dim strFullUrl As String

If String.IsNullOrEmpty(strRelativePath) Then
Return strRelativePath
End If



If strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase) Then
strFullUrl = strRelativePath
Else
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri

If (strFullUrl.IndexOf("ClientBin") > 0) Then
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath
Else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath
End If
End If


Return strFullUrl
End Function
End Class

星期三, 6月 04, 2008

以loop透過取代變數名稱設定值

Dictionary variables = new Dictionary();

foreach( string temp in words )
{
variables[ temp + "_buy" ] = "4";
}

星期三, 3月 19, 2008

十捨五入文字輸出

//十捨五入文字輸出
public string toString(double d)
{
string str = string.Format("{0:###0.000}", d);
if (str.ToCharArray()[str.Length - 1] >= '5')
d += 0.005;


return string.Format("{0:###0.##}", d);
}

星期六, 3月 01, 2008

c# 控制滑鼠

[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, int extraInfo);
private enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}

//mouse move
public void myMoseMove(Point p)
{
mouse_event(MouseEventFlag.Move, p.X - Cursor.Position.X, p.Y - Cursor.Position.Y, 0, 0);
}

//mouse click
public void myMouseClick(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftDown, p.X, p.Y, 0, 0);
mouse_event(MouseEventFlag.LeftUp, p.X, p.Y, 0, 0);
}

//mouse down
public void myMouseDown(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftDown, p.X, p.Y, 0, 0);
}

//mouse up
public void myMouseUp(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftUp, p.X, p.Y, 0, 0);
}

//mouse double click
public void myMouseDoubleClick(Point p)
{
Cursor.Position = p;
myMouseClick(p);
myMouseClick(p);
}

星期五, 2月 29, 2008

c# 跨執行序處理

CheckForIllegalCrossThreadCalls = false;


如果不是要做啥特殊的處理,就暫時這樣做了

星期日, 2月 24, 2008

c# 執行檔案

const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_ACCESS_DENIED = 5;

try
{
process1.StartInfo.FileName = Path;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
}
catch (Win32Exception ee)
{
if (ee.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(ee.Message + ". Check the path.");
}

else if (ee.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(ee.Message +
". You do not have permission to print this file.");
}
}

c# 畫透明圖

private void drawTransparentImage()
{
Graphics g = pictureBox1.CreateGraphics();

Bitmap bitmap = new Bitmap(Resources.PDA1, pictureBox1.Size);
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, f, 0},
new float[] {0, 0, 0, 0, 1}};

ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);

g.Dispose();
}

c# 計算兩張圖箭頭連線的點

private Point calpointer(PictureBox pics, PictureBox picd)
{
Point pd = new Point(picd.Location.X + picd.Size.Width / 2, picd.Location.Y + picd.Size.Height / 2);
Point ps = new Point(pics.Location.X + pics.Size.Width / 2, pics.Location.Y + pics.Size.Height / 2);
Point p1 = new Point();
Point p2 = new Point();

float m = (float)(pd.Y - ps.Y) / (pd.X - ps.X);
float d1, d2;
float x1 = 0, x2 = 0, y1 = 0, y2 = 0;


if (ps.X == pd.X)
{
if (ps.Y > pd.Y)
return new Point(pd.X, picd.Location.Y + picd.Size.Height);
else
return new Point(pd.X, picd.Location.Y);
}
else if (ps.Y == pd.Y)
{
if (ps.X > pd.X)
return new Point(picd.Location.X + picd.Size.Width, pd.Y);
else
return new Point(picd.Location.X, pd.Y);
}
else if (ps.X <= pd.X && ps.Y <= pd.Y)
{
x1 = pd.X - picd.Size.Width / 2;
y1 = pd.Y - (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X - picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y - picd.Height / 2;

}
else if (ps.X >= pd.X && ps.Y <= pd.Y)
{
x1 = pd.X + picd.Size.Width / 2;
y1 = pd.Y + (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X - picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y - picd.Height / 2;
}
else if (ps.X >= pd.X && ps.Y >= pd.Y)
{
x1 = pd.X + picd.Size.Width / 2;
y1 = pd.Y + (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X + picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y + picd.Height / 2;
}
else if (ps.X <= pd.X && ps.Y >= pd.Y)
{
x1 = pd.X - picd.Size.Width / 2;
y1 = pd.Y - (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X + picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y + picd.Height / 2;
}

p1.X = (int)x1;
p1.Y = (int)y1;
p2.X = (int)x2;
p2.Y = (int)y2;

d1 = (p1.X - pd.X) * (p1.X - pd.X) + (p1.Y - pd.Y) * (p1.Y - pd.Y);
d2 = (p2.X - pd.X) * (p2.X - pd.X) + (p2.Y - pd.Y) * (p2.Y - pd.Y);



if (d1 > d2)
return p2;
else
return p1;

}

星期六, 2月 23, 2008

c# 自畫圖片在PictureBox上

PictureBox pic = new PictureBox();
Bitmap bitmap = new Bitmap(Resources.Ellipse_blue_1); //隨便一張圖
Graphics g = Graphics.FromImage(bitmap);
pic.Image = bitmap;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
g.Dispose();

c# 計算字串的寬度

public static int CharWidth(string str, System.Drawing.Font font)
{
string target = str;
System.Windows.Forms.Control control = new System.Windows.Forms.Control();
System.Drawing.Graphics g = control.CreateGraphics();
System.Drawing.SizeF sizef = g.MeasureString(target, font);
g.Dispose(); // Necesary to destroy the graphics object
int charWidth = (int)sizef.Width; // gets the width of the size object.
return charWidth;
}