Copy มาจาก Internet ตั้งแต่ปีมะโว้น้องฯชาติที่แล้ว จนผมจำไม่ได้ว่าเอามาจากไหน
Code (VB.NET)
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class ImageHelper
' Convert an image to a Base64 string by using a MemoryStream. Save the
' image to the MemoryStream and use Convert.ToBase64String to convert
' the content of that MemoryStream to a Base64 string.
Public Shared Function ImageToBase64String(ByVal img As Image, ByVal imgFormat As ImageFormat)
Using ms As New MemoryStream
img.Save(ms, imgFormat)
Dim result As String = Convert.ToBase64String(ms.ToArray())
'ms.Close()
Return result
End Using
End Function
' Convert a Base64 string back to an image. Fill a MemoryStream based
' on the Base64 string and call the Image.FromStream() methode to
' convert the content of the MemoryStream to an image.
Public Shared Function ImageFromBase64String(ByVal base64 As String)
Using ms As New MemoryStream(Convert.FromBase64String(base64))
Dim result As Image = Image.FromStream(ms)
'ms.Close()
Return result
End Using
End Function
End Class
Date :
2013-09-19 09:45:58
By :
ผ่านมา
No. 2
Guest
Code (C#)
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public class ImageHelper
{
public static object ImageToBase64String(Image img, ImageFormat imgFormat)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, imgFormat);
string result = Convert.ToBase64String(ms.ToArray());
return result;
}
}
public static object ImageFromBase64String(string base64)
{
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
{
Image result = Image.FromStream(ms);
return result;
}
}
}
Anyone who use VFP8 and above. Try to Register this web service and you can create this one out easily.
1) Open Task Pane and go to XML Web Service from the menu bar. If you can not see, just click >> and you will see it.
2) Click on Register an XML Web Service link and fill the popup box with "http://www.deeptraining.com/webservices/weather.asmx?WSDL" without quote and then click Register and then Done buttons
3) You will see "Explore an XML Web Service" bar than will show option of "Weather".
4) You will see the "Sample Code:", just copy and paste in the program window.
5) Add the "GetWeather", the one that display in "Method" box after you have registered the web service.
6) You will have the code similar to my code below.
7) I have added the lparameter so that I can call and get the return back and save the prg as WS_GetWeather.prg.
For Example:
lcReturn = WS_GetWeather('Seattle') && This service only allow for US city.
*** It will return 'rain'
LPARAMETERS tcCity AS STRING
LOCAL loWeather AS "XML Web Service"
* LOCAL loWeather AS "MSSOAP.SoapClient30"
* Do not remove or alter following line. It is used to support IntelliSense for your XML Web service.
*__VFPWSDef__: loWeather = http://www.deeptraining.com/webservices/weather.asmx?WSDL , Weather , WeatherSoap
LOCAL loException, lcErrorMsg, loWSHandler
TRY
loWSHandler = NEWOBJECT("WSHandler",IIF(VERSION(2)=0,"",HOME()+"FFC\")+"_ws3client.vcx")
loWeather = loWSHandler.SetupClient("http://www.deeptraining.com/webservices/weather.asmx?WSDL", "Weather", "WeatherSoap")
* Call your XML Web service here. ex: leResult = loWeather.SomeMethod()
lcResponse = loWeather.GetWeather(tcCity)
CATCH TO loException
lcErrorMsg="Error: "+TRANSFORM(loException.Errorno)+" - "+loException.Message
DO CASE
CASE VARTYPE(loWeather)#"O"
* Handle SOAP error connecting to web service
CASE !EMPTY(loWeather.FaultCode)
* Handle SOAP error calling method
lcErrorMsg=lcErrorMsg+CHR(13)+loWeather.Detail
OTHERWISE
* Handle other error
ENDCASE
* Use for debugging purposes
MESSAGEBOX(lcErrorMsg)
FINALLY
ENDTRY
RETURN lcResponse
In wsdl file:
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://litwinconsulting.com/webservices/">
- <s:element name="GetWeather"> <-- This one is the available service
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="City" type="s:string" /> <-- This is what to pass and what is the type.
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="GetWeatherResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetWeatherResult" type="s:string" /> <-- This is returned value and what is the type.
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
- <wsdl:service name="Weather"> <-- This is the service name. (2nd parameter in loWSHandler.SetupClient)
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Provides weather forcasts for U.S. cities</wsdl:documentation>
- <wsdl:port name="WeatherSoap" binding="tns:WeatherSoap"> <-- This is the Port Name. (3rd parameter in loWSHandler.SetupClient)
<soap:address location="http://www.deeptraining.com/webservices/weather.asmx" /> <-- This is the URI. (1st parameter in loWSHandler.SetupClient)
</wsdl:port>
- <wsdl:port name="WeatherSoap12" binding="tns:WeatherSoap12">
<soap12:address location="http://www.deeptraining.com/webservices/weather.asmx" />
</wsdl:port>
</wsdl:service>
That is the way we consume Web Service from the other. We can Create our own web service and provide service to the other as well.
-------------
Everything is possible. It depends on how we treat them. Live with them and learn from them to gain benefit from them.