卡西卡的小寶庫
寶庫寶庫寶庫

C#產生動畫GIF

Posted In: , , . By 卡西卡

CodeProject: NGif, Animated GIF Encoder for .NET

優點:影像品質佳
缺點:速度較慢
Photobucket
Gif.Components.AnimatedGifEncoder e = new Gif.Components.AnimatedGifEncoder();
e.Start(@"c:\test.gif");
e.SetDelay(500);
//-1:no repeat,0:always repeat
e.SetRepeat(0);
for (int i = 0 ; i < 12; i++ )
{
    e.AddFrame( Image.FromFile( string.Format(@"F:\Downloads\map({0}).png", i) ) );
} // for
e.Finish();

HOWTO: create an animated GIF using .Net (C#) - Rick van den Bosch - Blog
優點:速度快,檔案小
缺點:影像品質較差,Microsoft GIF Animator及Adobe ImageReady CS2判定格式錯誤無法開啟(但IE和FF正常)。
Photobucket

 

其它資料:

 

Google Map API (6) 搜尋

Posted In: . By 卡西卡

關鍵:GClientGeocoder, getLocations
展示:http://klcintw.googlepages.com/google-map-GClientGeocoder.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps (台灣)</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key="
      type="text/javascript" for="http://klcintw.googlepages.com"></script>
   1:  
   2:     <script type="text/javascript">
   3:     //<![CDATA[
   4: var map = null;
   5: var geocoder = null;
   6: function load() {
   7:     // 檢查瀏覽器是否可使用 Google Map API
   8:     if ( GBrowserIsCompatible() ) {
   9:         map = new GMap2(document.getElementById("map"));
  10:         geocoder = new GClientGeocoder();
  11:         
  12:         // 設定地圖中心點
  13:         map.setCenter(new GLatLng(25.036772,121.520269), 14);
  14:     } // if
  15: }
  16:  
  17: // showLocation() is called when you click on the Search button
  18: // in the form.  It geocodes the address entered into the form
  19: // and adds a marker to the map at that location.
  20: function showLocation() {
  21:     var address = document.forms[0].q.value;
  22:     geocoder.getLocations(address, addAddressToMap);
  23: }
  24:     
  25: // addAddressToMap() is called when the geocoder returns an
  26: // answer.  It adds a marker to the map with an open info window
  27: // showing the nicely formatted version of the address and the country code.
  28: function addAddressToMap(response) {
  29:     map.clearOverlays();
  30:     if (!response || response.Status.code != 200) {
  31:         alert("查無資料!\n Sorry, we were unable to geocode that address");
  32:     } // if
  33:     else {
  34:         place = response.Placemark[0];
  35:         point = new GLatLng(place.Point.coordinates[1],
  36:                             place.Point.coordinates[0]);
  37:         marker = new GMarker(point);
  38:         map.addOverlay(marker);
  39:         marker.openInfoWindowHtml(place.address + '<br>' +
  40:           '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  41:     } // else
  42: }
  43:     
  44: // findLocation() is used to enter the sample addresses into the form.
  45: function findLocation(address) {
  46:     document.forms[0].q.value = address;
  47:     showLocation();
  48: }     
  49:     //]]>
  50:   
</script>
  </head>
  <body onload="load()" onunload="GUnload()">
  <h1>GClientGeocoder</h1>
    <form action="#" onsubmit="showLocation(); return false;">
      <p>
        <b>地址:</b>
        <input type="text" name="q" value="古亭國小" class="address_input" size="40" />
        <input type="submit" name="find" value="Search" />
      </p>
    </form>  
    <div id="map" style="width:500px; height:350px"></div>
    <ul>
    <li><a href="javascript:findLocation('台北市羅斯福路三段201號');">古亭國小(以地址尋找)</a></li>
    <li><a href="javascript:findLocation('古亭國小');">古亭國小(以名稱尋找)</a></li>
    </ul>
  </body>
</html>

snap026

 

  • 一般組態
    web.config
    <?xml version="1.0"?>
    <configuration>
      <appSettings file="out.config">
        <add key="in_string" value="IN"/>
      </appSettings>
    </configuration>

    out.config
    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
      <add key="out_string" value="out" />
    </appSettings>

    讀取組態:
    System.Configuration.ConfigurationManager.AppSettings["in_string"]
    System.Configuration.ConfigurationManager.AppSettings["out_string"]
  • 連線組態

    讀取組態:
    System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString
    內部
    <?xml version="1.0"?>
    <configuration>
      <connectionStrings
        <add name="default" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    </configuration>

    外部
    <!-- web.config -->
    <?xml version="1.0"?>
    <configuration>
      <connectionStrings configSource="conn.config" />
    </configuration>

    <!-- conn.config -->
    <connectionStrings>
      <add name="default" connectionString ="data=out;uid=;pid=;"/>
    </connectionStrings>

ASP.NET 1.1 是用 System.Configuration.ConfigurationSettings.AppSettings["default"]

 

Customers.aspx
=AJAX, 向web service請求資料。
[script]
function displayCustomers(result) {
$get("customers").innerHTML = result;
}
[script]

[body]
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/CustomerService.asmx" />
</Services>
</asp:ScriptManager>

Enter Country: <input id="Country" type="text" />
<a href="javascript:CustomerService.GetCustomersByCountry($get('Country').value, displayCustomers)">Show Customers</a>
<div id="customers"></div>
[/body]

CustomerService.cs
=這個AJAX用的web service是宣告 System.Web.Script.Services.ScriptService
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CustomerService : System.Web.Services.WebService {

[WebMethod]
public string GetCustomersByCountry(string country)
{
NorthwindTableAdapters.CustomersTableAdapter customersAdapter = new NorthwindTableAdapters.CustomersTableAdapter();
Northwind.CustomersDataTable customers = customersAdapter.GetCustomersByCountry(country);

if (customers.Rows.Count > 0)
return ViewManager.RenderView("~/App_Views/Customers.ascx", customers);
else
return ViewManager.RenderView("~/App_Views/NoCustomers.ascx", null);
}
}

Customers.ascx.cs
=View
using System;

public partial class App_Views_Customers : System.Web.UI.UserControl
{
public object Data;

protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = Data;
Repeater1.DataBind();
}
}

ViewManager.cs
=Controller
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Reflection;

public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}

public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl) pageHolder.LoadControl(path);

if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField("Data");

if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception("View file: " + path + " does not have a public Data property");
}
}

pageHolder.Controls.Add(viewControl);

StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);

return output.ToString();
}
}
 


ASP.NET 1.1 只能用在.aspx上
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute("~/ajax.aspx", output);
return output.ToString();

參考資料: